Understanding Hibernate, JPA and EJB 3
The Term JPA- Java Persistence API is also used when referring the Hibernate and we know that the JPA is part of EJB3. Following section gives a brief Idea about the 3 terms and explains how they are related.
Hibernate :
The Object-Oriented Programming is widely used programming techniques to develop any application. Persistence is the basic concept in application development. Without data saving the applications can do little. The Relational Database Management System is widely used to save/persist the application specific data.
When your software system is developed using an object-oriented programming language and data persisted in RDBMS, there is always a mismatch. You can’t save data from a java object / bean directly to the database table. You usually have to extract data from beans / objects, prepare the SQL statements and should be persisted /saved to the columns of the table. In such scenarios much of the persistence work like saving data and fetching data is done by writing complex query by the developers. They copy values from JDBC result set to java objects and assemble, handle multiple joins etc. Major disadvantage in such scenarios is that the developer’s time is not fully utilized for the business logic and productivity will be affected. Changes in one part of db tables will affect widely on the application. So the code will be less maintainable. Also writing direct SQL statements into the code makes it directly dependent on the vendor of your DB.
The solution for these would be a middleware between the DB and your java application, which save data stored in the beans directly to the tables without much hassle. Object / Relational Mapping is a technique used to do so. The term used for the concept where we can relate the bean properties and table columns so that, a middleware can do the saving and loading operations for the user.
Hibernate is such a middle ware. It helps mapping the objects to tables and manages the persistence. In Hibernate you can specify the relationship between the table and the java bean by mapping the table columns to the bean properties using xml files and the objects can be saved directly to the database. Hibernate core can be used will all java versions.
Hibernate and EJB 3 can’t be compared. Both are different concepts. EJB 3.0 is a specification of architecture for developing and deploying server side managed components (beans). The architecture manages the lifecycle of beans and also provides services like security, transactions, persistence management, Timer services etc.
EJB 3 specification has many parts. Java Persistence API specification is one. This specification deals with the object / relational mappings, persisting the entities, defining the persistence management interfaces etc. The EJB 3 specification made the Java Persistence engine independent from the EJB container. According to it the persistence engine should be pluggable to the container so that users don’t rely on single vendor for persistence. Also persistence engine should be able to run outside the EJB container so that it can be used for simple java programs without an EJB server.
Hibernate implemented JPA on top of Hibernate core. One can map the java objects using java persistence API’s annotations for entity etc and without an XML mapping file the object becomes persistent. Hibernate uses its own objects like Session, Transaction and Query to persist data in its own way. But JPA specifies separate Interfaces for managing the entities and querying. Hibernate also implements these JPA interfaces in Hibernate Entity Manager. So if your beans are mapped using JPA annotations and you use an Entity manager implementation then your code becomes portable across multiple persistence products which implements JPA specification. (One can use same entities on EJB 3.0 server also)








