Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB3 and JPA

 
Rahul Ba
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

then,

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é
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic