This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes EJB3 and JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3 and JPA" Watch "EJB3 and JPA" New topic

EJB3 and JPA

Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 206
I have a question... I am not sure whether my question is right
or not, but It is making me to think. Hence asking

We have Java Persistence API which is defined in the javax.persistence package.


1) why we need EJB3, Hibernate to achieve persistence in Java.

2) How to write a code which achives persistence without using EJB3, Hibernate? any eg.

Thanks in advance.
[ November 19, 2008: Message edited by: Rahul Ba ]
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

A little background first. In the EJB2.x time, the EJB container was doing all the dirty work : managing session beans, message-driven beans and entity beans. From EJB3, managing session beans, message-driven beans was left to the EJB container, whereas entity beans were removed from the core EJB specification. The management of the new Entities is now the responsability of JPA containers. If you plan to use EJB3 and JPA, you need both an EJB container and a JPA container.

why we need EJB3, Hibernate to achieve persistence in Java.

You don't. You can use JPA with J2SE. You can use Hibernate (JPA container) only to persist entities.
[ November 19, 2008: Message edited by: Christophe Verre ]

[My Blog]
All roads lead to JavaRanch
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16470

JPA is actually part of the EJB3 specification, but in a strange twist of logic on someone's part, it's allowed to be implemented in non-EJB environments.

You don't actually need EJB or any other ORM framework (Hibernate, JDO, etc.) to achieve persistence in Java. You can use raw JDBC. However the ORM frameworks provide a boost in productivity, make it easier for people whose primary skills are in Java and not SQL to work with databases, and can actually be more efficient when you scale up than manual JDBC code generally permits.

JPA is a convergence of the 3 major ORM platforms: EJB, Hibernate and JDO. It offers the advantage that you can take JPA code and back it with EJB when using a JEE container and also use the same code in non-EJB environments like J2SE applications.

Customer surveys are for companies who didn't pay proper attention to begin with.
rocket mass heater kickstarter
subject: EJB3 and JPA