Help

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.

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.