问题
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.
回答1:
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.
回答2:
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.
回答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.
来源:https://stackoverflow.com/questions/8952730/how-does-ejb-and-jpa-relate