Hello all
I’m using hibernate 3.6 in a JSE 2-tier environment, configured with annotated classes. Lazy loading doesn’t work, I get a SessionIsClosed exception.
I scanned different webpages and forums and I learned that I have to keep the session open in order to make lazy loading work. The problem is that I don’t have any session because I use the JPA pattern with EntityManagers, and the EntityManagerFactory doesn’t know how to return the current (open) EntityManager. I tried some implementations with a static EnitityManager that remains open, but this didn’t work either.
Is there any simple solution to this problem except of switching to J2EE or using the Hibernate SessionManager pattern?
Here is what a standard “select” method looks like:
Code:
static protected AbstractPersistentId dbSelect(Class<? extends AbstractPersistentId> cls, Integer id)
{
EntityManager entityManager = null;
EntityTransaction transaction = null;
AbstractPersistentId obj = null;
try
{
entityManager = EntityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
Logger.debug("Selecting one for class '" + cls.getName() + "', id: " + id);
obj = entityManager.find(cls, id);
Logger.info("Selected one for class '" + cls.getName() + "', found: " + obj);
transaction.commit();
}
catch (RuntimeException ex)
{
if (transaction != null) transaction.rollback();
throw(ex);
}
finally
{
if (entityManager != null) entityManager.close();
}
return obj;
}
I would very much appreciate a hint.
Many thanx in advance
:-Denis