Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject EJB into domain object with Java EE 6

How can I inject an EJB into a domain object (an JPA entity) with Java EE 6?

like image 394
deamon Avatar asked Jan 08 '10 09:01

deamon


3 Answers

In Java EE 6, CDI extends the concept of managed component to anything and EJB can be injected into a CDI managed bean (using the @Inject annotation). But while interaction between JPA and CDI has been considered, this has not been made part of the Java EE 6 specification(s). In other words, injection into a JPA entity is not possible.

See also

  • CDI injection broken between POJOs
like image 54
Pascal Thivent Avatar answered Sep 20 '22 14:09

Pascal Thivent


EJB3 client injection applies to "managed classes" such as Servlets and EJBs (and JSF page code etc).

The JPA objects are not, I believe, managed in this sense. So I think you would be back to pre-EJB3 techniques, doing JNDI lookup etc.

However as has been observed there may be some tangle in your hierarchy of responsibilities if you need to do this. It would be interesting if you posted a question about your overall design so that we could think about the design issues. My default position is that EJBs use JPA domain objects, JPA objects don't use EJBs.

like image 4
djna Avatar answered Sep 22 '22 14:09

djna


You would typically not do this simply because domain objects come out of the database as opposed to the container and therefore injecting services isn't as straight forward.

This does not mean however that you should not do this.

You know what your system is meant to achieve and which other systems it interacts with. This knowledge will effect the decision I'd imagine.

See Active Record link. As I meanted in my comment typically small systems would choose this route.

like image 1
JARC Avatar answered Sep 19 '22 14:09

JARC