36.4. Entities
In Seam 2, it is quite common to use JPA entities as Seam-managed objects. As an example, consider the User entity from the Seam 2 booking application. Such an entity is instantiated by Seam and stored in the session context. Its values are bound to a form in a JSF page using EL expressions and filled by an application user. Finally, the managed object is passed to the
EntityManager and persisted in the database. Since Seam 2 stores direct references to bean objects in its contexts, this approach works fine for non-intercepted entities, as entities usually are.
But CDI uses a different memory model. Every time an application code accesses a reference to a CDI-managed bean stored in a normal context, it actually accesses a dynamic client proxy. Since proxy classes are different from the original classes of the entity, they cannot be used in the
persist() method of the EntityManager. Therefore, you cannot use entities as CDI managed beans.
Although this may seem like a major limitation, it is not the case. Most of the time, there is no need for JPA entities to support dependency injection nor do they need to be an Interceptor. The only services really useful to entities is storing them in a context. CDI supports this in the form of a
Producer field or method. The common approach is to use a stateful manager object for instantiating and holding the reference to a scoped entity. See the RegisterAction component of the Seam 2 booking application as an example.