How to use EntityManagerPerRequestHelper in Vaadin 7

Hello!
How can we use the EntityManagerPerRequestHelper class in Vaadin 7. Using vaading 6 I used to extend the Application class like this
[i]

public class MyApplication extends Application implements
HttpServletRequestListener
[/i]

initialize the object in the constructor


entityManagerPerRequestHelper = new EntityManagerPerRequestHelper();

and then implement the Request methods.

[i]
@Override
public void onRequestStart(HttpServletRequest request,
HttpServletResponse response) {
entityManagerPerRequestHelper.requestStart();

@Override
public void onRequestEnd(HttpServletRequest request,
HttpServletResponse response) {
entityManagerPerRequestHelper.requestEnd();
[/i]

What is the pattern for using EntityManagerPerRequestHelper in Vaadin 7 beta 2.

Thank you

same problem for me…

also for me!

Hi guys,

As Vaadin 7 removed the HttpServletRequestListener, you must now use servlet filters for this. The documentation has still to be updated before final release, but an example can be found in
the LazyHibernate integration test
.

The example linked above basically creates a servlet filter that creates a new entity manager for each request and stores it in a ThreadLocal in the LazyHibernateEntityManagerProvider (also in the example). After this, each JPAContainer that is created should use the LazyHibernateEntityManagerProvider by calling

LazyHibernateEntityManagerProvider entityManagerProvider = new LazyHibernateEntityManagerProvider();
myContainer.getEntityProvider().setEntityManagerProvider(entityManagerProvider);

or alternatively setting it up when creating the EntityProvider.

In a real-world application, you might want to check what the URI is before creating entity managers, so that they are not unnecessarily created, even though this is a light operation.

HTH,
/Jonatan