Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship between EJB 3.0 and JPA?

Tags:

That may seem obvious but I've seen contradictory statements: Is JPA part of EJB 3.0? I'm no specialist and it's quite confusing for me.

If so, JPA manipulates Entity Beans? These entity beans being the interface between the persistence layer and the business layer implementing the logic with stateless beans?

The underlying question for me is how to implement a "search for user based on various criteria" function, where the "search" request -its string representation- should be built? I mean, if JPA is not part of EJB, my beans shouldn't be aware of the data model, right?

Where is the boundary?

like image 723
LB40 Avatar asked Oct 30 '11 15:10

LB40


People also ask

Is JPA part of Java EE?

JPA was adopted as an independent project of Jakarta EE in 2019. The current release as of this writing is JPA 3.1. Popular JPA implementations like Hibernate and EclipseLink now support JPA 3.

Is JPA a relational database?

Yes. JPA is a framework that provides an object / relational mapping.

What is JPA in Enterprise Java?

The Java™ Persistence API (JPA) provides a mechanism for managing persistence and object-relational mapping and functions since the EJB 3.0 specifications. The JPA specification defines the object-relational mapping internally, rather than relying on vendor-specific mapping implementations.

What is JPA and why do we need it?

JPA allows you to avoid writing DDL in a database specific dialect of SQL. Instead you write "mappings" in XML, or using Java annotations. JPA allows you to avoid writing DML in the database specific dialect of SQL. JPA allows you to load and save Java objects and graphs without any DML language at all.


2 Answers

Is JPA part of EJB 3.0 ?

Yes and no... Yes because every application server claiming to implement EJB 3.0 spec must also provide JPA implementation. No because JPA can be easily outside of EJB, in standalone applications or Spring-managed ones.

JPA manipulates Entity Beans ?

Entity beans was a scary idea in pre-3.0 EJBs. JPA uses the term entities to distinguish itself from the disgraceful history. But yes, JPA entities are a way to map database tables to plain Java objects. In principle changes made to object are propagated to database and vice-versa (oversimplification).

As I said, JPA does not have any dependency on EJB (considered as stateless and stateful session beans) and the other way around. But there is nothing preventing you from using JPA in EJB.

In your scenario you'll have a stateless EJB constructing the query and interacting with the database via JPA. Technically speaking you will call methods on EntityManager injected to your bean:

@Stateless public class SearchService {      @PersistenceContext     private EntityManager em;      public List<User> findUsersBornAfter(Date date) {         return em.             createQuery("SELECT u FROM User u WHERE u.birthDate > :birthDate ORDER BY name").             setParameter("birthDate", date).             getResultList();     } } 

As you can see the business layer is aware of the data model (obviously), but there is no dependency on EJB/business services as far as entities are concerned. In this example JPQL (query) is formed in the service layer and User is a JPA entity. Calling getResultList() causes the JPA provider to translate JPQL to SQL, run the query and transalte the results back to User object instances.

Is the border between EJB and JPA clear now?

like image 173
Tomasz Nurkiewicz Avatar answered Oct 05 '22 17:10

Tomasz Nurkiewicz


A couple of notes:

  • when it comes to the JSR, JPA 1.0 was part of EJB 3.0. See here , JPA 2.0 is a separate JSR (see here)
  • "entity beans" are EJB pre-3.0, and they are luckily dead. They are succeeded by JPA.
  • when it comes to dependencies - JPA does not depend on EJB, and EJB does not depend on JPA. However, EJB can handle injection of JPA entity manager
  • you can use either of them standalone
  • each application server supports both
like image 24
Bozho Avatar answered Oct 05 '22 18:10

Bozho