views:

28

answers:

1

I just configured @PersistenceContext(type=PersistenceContextType.EXTENDED) on my DAO classes to get lazy loading working on the view layer (needed to get it not closing the session for this) but I am having issues with 1 level cache (I imagine).. Because the object is cached even with my second level cache settings off. After turned off the "type=PersistenceContextType.EXTENDED" I got the system not caching the object but dived into the lazy loading issues again.

//@PersistenceContext(type=PersistenceContextType.EXTENDED)
@PersistenceContext private EntityManager entityManager;

Is there any way to control this? I really need lazy loading but don't want to get my object stored on a 1 level cache every time.

thanks

+1  A: 

Unless you really want to use an extended persistence context and really understand what it means (I don't mean to be pedantic but this is important as an extended entity manager has different threading, lifecycle, and memory management semantics), my suggestion would be to use the usual OEIV (Open EntityManager in View) pattern to solve your lazy loading issue.

Spring provides two implementations of this pattern with a filter (OpenEntityManagerInViewFilter) and an interceptor (OpenEntityManagerInViewInterceptor). From the javadoc:

Intended for the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.

Pascal Thivent
hum, it will make my persistence alive with the thread created by a request, don't know if it can cause problems for me, but is probably better than PersistenceContextType.EXTENDED. Thanks