How can I inject an EJB into a domain object (an JPA entity) with Java EE 6?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With