Help

Switch Workspace

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

FAQ Category:

Both the @PersistenceContext annotation and the @In annotation declare an injection point for an EntityManager. However, where they get that EntityManager from, and therefore how it is managed, is very different.

Java EE container injection

The @PersistenceContext annotation added to a field or setter method of type EntityManager injects a container-managed persistence context (EntityManager). If there is only one persistence unit in the application, the unitName attribute is not required.

@PersistenceContext
private EntityManager em;

If there is more than one persistence unit in the application, the unitName attribute must match the name assigned to the persistence unit in the persistence unit descriptor:

<persistence-unit name="reportingDatabase">
   ...

</persistence-unit>

Injected as follows:

@PersistenceContext(unitName = "reportingDatabase")
private EntityManager em;

In both cases, the name of the field or setter method is arbitrary.

This injection can only be used on a Java EE component, such as an EJB session bean or a JSF managed bean. If the Java EE compoennt is also a Seam component, then Seam will post-process the injection by wrapping the reference in a proxy, allowing EL embedded in JPQL to be interpreted and support Hibernate Search queries.

The container-managed persistence context can be marked as either TRANSACTION or EXTENDED. A TRANSACTION persistence context is closed when the transaction ends, whereas the EXTENDED persistence context is closed when the component is destroy (hence it is bound to the life time of the component).

Seam injection

The @In annotation added to a field or setter method of type EntityManager will look for an inject a Seam-managed persistence context whose name matches the name of the field or bean property name for the setter method.

@In
private EntityManager entityManager;

A Seam-managed persistence context is created and managed by Seam and stored directly in the conversation scope. Therefore, all Seam-managed persistence contexts are inherently extended, bound to the life time of the conversation.

Being a first class citizen of the application, a Seam-managed persistence context can be injected into any Seam component, whether it be a JavaBean or session bean component, making it easy to share the reference. This is in contrast to the container-managed persistence context, which is subject to complex propagation rules. Therefore, the Seam-managed persistence context is inherently more flexible than the container-managed complement.

Another key benefit of using a Seam-managed persistence context is that it allows Seam to activate Hibernate's manual flushing, which is not possible when using a container-managed persistence context.

Summary and additional reading

In summary, while both injections give you an EntityManager that understands EL in queries (assuming the Java EE component is a Seam component), it's the owner of the EntityManager that is different.

There is extensive detail about the difference between Seam-managed and container-managed persistence contexts in chapter 9 of Seam in Action. You can also find a wealth of information about persistence contexts in Java Persistence with Hibernate.

5 comments:
 
28. Jul 2009, 12:35 CET | Link

I am bit sad to read that this injection can only be utilized on a Java EE constituent, for example an EJB session bean or a JSF managed bean. I hope you will solve this drawback soon.

 
30. Aug 2009, 14:00 CET | Link

You didn't really read the text, did you? It clearly states: a Seam-managed persistence context can be injected into any Seam component, whether it be a JavaBean or session bean component.

 
01. Nov 2009, 10:33 CET | Link
JanHH

How does the seam managed persistence context behave when injected into a component with a scope different from conversation, and with no conversation currently running?

 
21. Dec 2009, 10:22 CET | Link

Hi as well as JanHH I want to know how should seam injected EntityManager be used in components living in different scopes. Right now for me it's some kind of magic. I writing component, got NullPointerException on the first attempt to access entityManager and try to change component scope until it fixed. Is there any useful information to understand how should I use EntityManager in component living in different scopes?

 
15. Jan 2010, 23:14 CET | Link

So manual flushing is only possible if you use Hibernate as the JPA implmentation? I'm using EclipseLink so I suppose I have no chance of getting manual flushing to work even if I switch to Seam injection from Container injection?

Post Comment