Lazy Loading with hibernate

I´m developing a Vaadin webapp using JPAContainer with hibernate 4.3.
I followed the instructions of this link (https://vaadin.com/book/-/page/jpacontainer.hibernate.html) 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:

[code]

try {
entityManagerProvider.getEntityManager().getTransaction().begin();

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

    for (SubEntity subEntity : subEntitiesList) {
        subEntity.doSomething();
        entityManagerProvider.getEntityManager().merge(subEntity);
    }
    entityManagerProvider.getEntityManager().flush();
    entityManagerProvider.getEntityManager().getTransaction().commit();
} catch (Exception ex) {
    entityManagerProvider.getEntityManager().getTransaction().rollback();
}

//datasource is JPAContainer<MyEntity>  type
datasource.removeItem(index);

[/code]
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:


1)
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


2)
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).


3)
Use Hibernate’s load time weawing by enabling compile time bytecode instrumentation. There’s discussion about that in Hibernates manual: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch20.html#performance-fetching-lazyproperties.


4)
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.