JPAContainer hitting hibernate LazyInitializationException

I’m using JPAContainer 1.2.1 for the first time and I’m hitting this exception:


Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: vortex.model.Person.addresses, no session or session was closed
	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
	at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
	at org.hibernate.collection.PersistentList.size(PersistentList.java:114)
	at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
	at vortex.model.tool.AddressTool.getPrimary(AddressTool.java:15)
...truncated....

The .getPrimary(AddressTool.java:15) call is “person.getAddresses()” where person is an EntityItem from JPAContainer(Person.class)
and “getAddresses()” is a List of Address objects.

The relevant code is approximately:


container = new JPAContainer<Person>(Person.class);

	    logst.debug(m, "Creating EntityProvider");
	    MutableLocalEntityProvider<Person> entityProvider =
		    new MutableLocalEntityProvider<Person>(Person.class,
			    				getEntityManager());
	    container.setEntityProvider(entityProvider);

// In a later function inside of a Table we get the person object that is selected
Person person = container.getItem(itemId).getEntity();

// Then we read various attributes of person and stuff those into different Components
person.getFirstName(); // String
person.getLastName(); // String

// Then we pass person.getAddresses() to AddressTool.getPrimary() which does
	if (addresses == null || addresses.size() == 0)
	    return null;
// The call to addresses.size() triggers the exception

Prior to the addresses.size() call I’ve put in debug code which shows the EntityManager isOpen=true and the EntityManager.Transaxtion isActive=true.

Attached are Person and Address classes.

I’m using Hibernate 3.5.7 as the entitymanager.

Anybody have any suggestions on this? Perhaps my JPA annotations in Person are wrong???

Thanks in advance.

mike
12000.java (11.4 KB)
12001.java (7.25 KB)

Hi Mike,

I’m not really sure if this would be an applicable solution in your situation, but you could try to keep the entities attached to the persistence context and see if that helps.

entityProvider.setEntitiesDetached(false);

… but if you do so, be sure to read the JPAContainer manual section related to it (transaction management).

In general, Hibernate does not work very nicely with lazy loading in a transaction-per-request model, but neither does it like long transactions. EclipseLink is more flexible with lazy loading.

I did not read the original post in detail, but in some cases simply using eager loading in some places is simpler if the amount of data to be loaded would be small.

“By default, Hibernate3 uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for most associations in the majority of applications.

Please be aware that access to a lazy association outside of the context of an open Hibernate session will result in an exception.”

Henri,

Your suggestion to

entityProvider.setEntitiesDetached(false);

did the trick! Thank you!

mike