Guice Persist @Transactional doesn't work with @Inject injected EntityManager (Hibernate)

1,420 views
Skip to first unread message

Alistair

unread,
Oct 17, 2010, 9:14:03 PM10/17/10
to google-guice
Hi

I've been experimenting with the snapshot of Guice 3 and the persist
API. I have followed the instructions and set up a ServletModule with
a filter:

filter( "/*" ).through( PersistFilter.class );

I've annotated a method with @Transactional, and in that class I
inject the EntityManager:

@Inject
private final EntityManager _em = null;

Then in the transactional method I create a new JPA entity:

@Transactional
public void test()
{
TestObject testObject = new testObject();
testObject.setDescription( "Hello, world" );
_em.persist( productComponent );
}

However, Hibernate never updates the database. You can see in the log
that it calls out to the sequence to generate the primary key, but
never executes the INSERT. If I check the value of
_em.getTransaction().isActive() in this method it returns false.

As a last resort I changed the code to match the code from the Guice
Persist unit tests and programmatically construct an EntityManager:

@Inject
private final Injector injector = null;

@Transactional
public void test()
{
EntityManager em = injector.getInstance( EntityManager.class );
TestObject testObject = new testObject();
testObject.setDescription( "Hello, world" );
em.persist( productComponent );
}

This now works correctly. Can anyone shed any light on why the first
approach with an injected EntityManager didn't work?

When I debugged the JpaLocalTxnInterceptor in Guice Persist, it did
report the transaction was active - this seemed to imply that there
were two EntityManagers but I don't understand why the @Inject syntax
causes a different instance of the EntityManager to be injected (e.g.
not the one that the @Transaction annotation relates to).

Dhanji R. Prasanna

unread,
Oct 17, 2010, 10:18:47 PM10/17/10
to google...@googlegroups.com
Probably because your class is a Singleton and EntityManager is not. You need to inject a Provider<EntityManager> instead.

Dhanji.


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Alistair

unread,
Oct 17, 2010, 10:36:16 PM10/17/10
to google-guice
Hi

It was the scope - I wasn't binding the class in request scope (was
using AbstractModule instead of ServletModule). Changing that fixed
the problem.

Thanks
Alistair

On Oct 18, 1:18 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> Probably because your class is a Singleton and EntityManager is not. You
> need to inject a Provider<EntityManager> instead.
>
> Dhanji.
>
> > google-guice...@googlegroups.com<google-guice%2Bunsubscribe@google groups.com>
> > .

mancereus

unread,
Jun 21, 2011, 4:55:32 AM6/21/11
to google...@googlegroups.com
Hi ,

i have the same problem and don't understand how to fix it. How did you fix it?
Bind which class to RequestScope?

i don't use 
filter( "/*" ).through( PersistFilter.class ); 

if i run the @transactional method from a java main method everything works fine.
if i run it inside a wicket web application, i get a second EntityManagerImpl and no insert is generated.

Any hints?

Thanks,
matthias
Reply all
Reply to author
Forward
0 new messages