Lazy Loading with hibernate

I´m developing a Vaadin webapp using JPAContainer with hibernate 4.3.
I followed the instructions of this link ( and implemented EntityManager-per-Request pattern successfully. That means the lazy loading works well with JPAContainer

However, every time I need to use pure JPA, it fails to lazy loads.

Here is an example code:


try {

    List<SubEntity> subEntitiesList = myEntity.getCollectionOfEntities(); //Fails to load

    for (SubEntity subEntity : subEntitiesList) {
} catch (Exception ex) {

//datasource is JPAContainer<MyEntity>  type

I have tried this solutions:

  1. Use hibernate.enable_lazy_load_no_trans
    It opened an session automatically, but it gave a serious warning:

    “Unable to close temporary session used to load lazy collection associated to no session”

Also, when I remove the Entity from the JPAContainer, I receive an error: org.hibernate.PersistentObjectException: detached entity passed to persist

Can someone give me some hints on how to deal with this problem?

What you’re seeing is quite common problem with pure JPA / Hibernate. The reason is what you’ve probably seen from the exceptions already: there is no active session available for loading nested entities from detached entity.

There are several solutions to this but none of them is very trivial or works out of the box. Most commonly the problem occurs when you’re using detached entities inside Vaadin’s data container or Field value and want to reference another entity through some getter. In this case the referenced entity is not available as the entity you’re talking to is already detached from the entity manager and is unable to fetch its nested content.

Few things you could think about:

Don’t bring entities to UI layer. Instead build DTO’s that contain the bits and pieces that you know you need. Don’t make 1:1 entity → DTO as this ruins the benefits of lazy loading but instead just read the properties you’re interested in

Make a service method from which you separately request the sub entity. Say you have an entities Invoice and InvoiceLine, instead of saying Invoice.getInvoiceLines() make an InvoiceService that is able to provide lines for given Invoice like this: InvoiceService.getInvoiceLinesFor(Invoice).

Use Hibernate’s load time weawing by enabling compile time bytecode instrumentation. There’s discussion about that in Hibernates manual:

If you’re using EJBs you could have a @Stateful EJB bean with extended mode entity manager that keeps the session open. This works nicely but I don’t know about scalability or clusterability, also this would require you to have EJB capable application server.

All in all, it could be argued that it’s bit of bad practice to have JPA entities directly in UI layer as they quite often belong to very different layers of application and doing that in many cases make clustering way more difficult. In JPAContainer you can use the EM-per-Request as you already said.

Hope these ideas help a bit.