How does EJB and JPA relate? [duplicate]
Asked Answered
S

3

18

I am reading the EJB 3 in Action book and I have the following question :
Is the POJO's you write and annotate with @Entity and so on also a EJB entity type?

I don't understand what JPA has to do with EJB. Isn't JPA a own specification now? The entities are also contained in a own persistence container. They talk about EJB 3 Java Persistence API etc. but I don't understand what entities has to do with EJB.

Sempach answered 21/1, 2012 at 11:48 Comment(1)
Your question is awesome.Hua
J
17

JPA has been designed to replace EJB2 entity beans, and has started as a part of the EJB3 specification.

Since it makes sense to also use JPA outside of an EJB container, it has now its own specification, but it's still related to EJB3, since a compliant EJB3 container has to provide a JPA implementation, which integrates into the transaction handling of the container.

Jesusa answered 21/1, 2012 at 11:57 Comment(2)
So even though it is not as tightly bound any longer, the EJB specification requires an implementation and that is why it is seen as a third component type in the EJB world?Sempach
EJB2 had "entity beans" which were a third type of component. EJB3 has JPA, which has "entities". But I don't think they're considered as "EJB components" anymore. They're just called JPA entities.Jesusa
B
9

Up until version 2.1 of the EJB specifications, an entity bean class had to implement the javax.ejb.EntityBean interface and provide an implementation for boilerplate methods such as ejbLoad, ejbStore, ejbActivate, and ejbPassivate.

EJB 3.0 adopted the JPA specification. The very notion of an entity bean was superceded by the simpler notion of a JPA entity. To create such entity, no interface implementation or boiler plate methods are required. The entity is a POJO that has the @Entity annotation.

Thus, in practice the use of "entity bean" EJBs in Java EE applications is dead (buried under JPA) as of EJB 3.

Bespangle answered 8/6, 2014 at 15:39 Comment(0)
L
3

You are right. JPA has more to do than only supporting EJB. Thats the reason why JPA became a separate JSR or specification. EJB uses or enables the usage of JPA in its specification, simply because JPA is a good standard. You can now switch between JPA vendors without changing your code if designed properly.

EJB specification can be used independent of JPA (although JPA has been included as a part of EJB spec) and likewise JPA can be used for many more stuff outside the EJB spec. Nevertheless, EJB specification enables the injection of JPA Entitiy Manager (and its usage) into its beans very easily which makes programming easier. Ofcourse this can now be achieved easily using a new JSR on CDI :-).

All the application server that supports EJB spec, should support JPA also. You can see this thread for more information.

Lashawnda answered 21/1, 2012 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.