I am new to OpenJPA and run into a little confusion over
auto detaching.
My setup.
Hessian servlet [1] which does some JPA and returns an
object. This acts as the server for a remote Swing client.
In my persistence.xml [2] I have
<property name="openjpa.AutoDetach" value="close, commit"/>
Which I understand should detach objects when a TX commits
or the entity manager closes.
If I don't manually call em.detach() I get lots of problems
when hessian tries to serialize the object for wire transfer.
Have I miss configured my persistence.xml or is there some
other (simpler) way of detaching objects without having to
manually call em.detach()?
Peter.
[1] Sample Servlet function.
public Account loadAccount(Integer accountId) {
EntityManager em = getEntityManagerBeginTx();
Account res = null;
try {
res = em.find(Account.class, accountId);
res = detach(em, res); // this makes it work
} finally {
returnEntityManager(em);
}
return res;
}
protected void returnEntityManager(EntityManager em) {
if (em.getTransaction().isActive()) {
em.flush();
em.getTransaction().commit();
}
em.close();
}
protected <T> T detach(EntityManager em, T pc) {
return ((OpenJPAEntityManager)em).detach(pc);
}
[2] persistence.xml (edited to remove class listing)
<?xml version="1.0"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="StarjarEnterpriseOpenjpaPU">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>jdbc/StarjarEnterprise5DS</jta-data-source>
<class>com.starjar.starjarenterprise5.domain.Account</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.Log" value="DefaultLevel=INFO,
Runtime=ERROR, Tool=ERROR, SQL=ERROR, DataCache=ERROR"/>
<property name="openjpa.AutoDetach" value="close,
commit"/>
</properties>
</persistence-unit>
</persistence>
--
Peter Henderson
Director Starjar Limited.
Mobile: +44 (0) 778 233 8645
Email: peter.hender...@starjar.com
Web: www.starjar.com