The Spring Framework

org.springframework.orm.jpa
Interface JpaCallback


public interface JpaCallback

Callback interface for JPA code. To be used with JpaTemplate's execute method, assumably often as anonymous classes within a method implementation. The typical implementation will call EntityManager CRUD to perform some operations on persistent objects.

Note that JPA works on bytecode-modified Java objects, to be able to perform dirty detection on each modification of a persistent instance field. In contrast to Hibernate, using returned objects outside of an active EntityManager poses a problem: To be able to read and modify fields e.g. in a web GUI, one has to explicitly make the instances "transient". Reassociation with a new EntityManager, e.g. for updates when coming back from the GUI, isn't possible, as the JPA instances have lost their identity when turned transient. This means that either value objects have to be used as parameters, or the contents of the outside-modified instance have to be copied to a freshly loaded active instance on reassociation.

Since:
2.0
Author:
Juergen Hoeller
See Also:
JpaTemplate, HibernateCallback

Method Summary
 Object doInJpa(javax.persistence.EntityManager em)
          Gets called by JpaTemplate.execute with an active EntityManager.
 

Method Detail

doInJpa

Object doInJpa(javax.persistence.EntityManager em)
               throws javax.persistence.PersistenceException
Gets called by JpaTemplate.execute with an active EntityManager. Does not need to care about activating or closing the EntityManager, or handling transactions.

Note that JPA callback code will not flush any modifications to the database if not executed within a transaction. Thus, you need to make sure that JpaTransactionManager has initiated a JPA transaction when the callback gets called, at least if you want to write to the database.

Allows for returning a result object created within the callback, i.e. a domain object or a collection of domain objects. A thrown RuntimeException is treated as application exception, it gets propagated to the caller of the template.

Parameters:
em - active EntityManager
Returns:
a result object, or null if none
Throws:
javax.persistence.PersistenceException - in case of JPA errors
See Also:
JpaTemplate.execute(org.springframework.orm.jpa.JpaCallback), JpaTransactionManager

The Spring Framework

Copyright © 2002-2006 The Spring Framework.