Wednesday, March 27, 2013

Spring @Transactional boundaries


InvalidDataAccessApiUsageException: detached entity passed to persist:

I experienced the exception above recently and thought it was simply, something like one of the following links:
http://stackoverflow.com/questions/2441598/detached-entity-passed-to-persist-error-with-jpa-ejb-code
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html

Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

In fact though it was because I was executing the queries across two different transactions and once I added @Transactional to the service method they were being executed in -- it changed to the same transaction.

http://stackoverflow.com/questions/6363683/spring-transactional-boundaries
http://forum.springsource.org/showthread.php?109091-JpaRepository-save()-gt-detached-entity-passed-to-persist
http://forum.springsource.org/showthread.php?75646-exception-detached-entity-passed-to-persist

1 comment: