Closed. This question is opinion-based. It is not currently accepting answers.
Closed 4 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
I understand that JPA 2 is a specification and Hibernate is a tool for ORM. Also, I understand that Hibernate has more features than JPA 2. But from a practical point of view, what really is the difference?
I have experience using iBatis and now I'm trying to learn either Hibernate or JPA2. I picked up Pro JPA2 book and it keeps referring to "JPA provider". For example:
If you think a feature should be standardized, you should speak up
and request it from your JPA provider
This confuses me so I have a few questions:
Using JPA2 alone can I fetch data from DB by simply annotating my POJO's
Is JPA2 supposed to be used with a "JPA Provider" e.g TopLink or Hibernate? If so, then what's the benefit of using JPA2 + Hibernate as compared to JPA2 alone, or compared to Hibernate alone ?
Can you recommend a good practical JPA2 book. "Pro JPA2" seems more like a bible and reference on JPA2 (It doesn't get into Queries until the later half of the book). Is there a book that takes a problem/solution approach to JPA2?
As you state JPA is just a specification, meaning there is no implementation. You can annotate your classes as much as you would like with JPA annotations, however without an implementation nothing will happen. Think of JPA as the guidelines that must be followed or an interface, while Hibernate's JPA implementation is code that meets the API as defined by the JPA specification and provides the under the hood functionality.
When you use Hibernate with JPA you are actually using the Hibernate JPA implementation. The benefit of this is that you can swap out Hibernate's implementation of JPA for another implementation of the JPA specification. When you use straight Hibernate you are locking into the implementation because other ORMs may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.
For a more detailed description read my blog entry.
JPA is the dance, Hibernate is the dancer.
Some things are too hard to understand without a historical perspective of the language and understanding of the JCP.
Often there are third parties that develop packages that perform a function or fill a gap that are not part of the official JDK. For various reasons that function may become part of the Java JDK through the JCP (Java Community Process)
Hibernate (in 2003) provided a way to abstract SQL and allow developers to think more in terms of persisting objects (ORM). You notify hibernate about your Entity objects and it automatically generates the strategy to persist them. Hibernate provided an implementation to do this and the API to drive the implementation either through XML config or annotations.
The fundamental issue now is that your code becomes tightly coupled with a specific vendor(Hibernate) for what a lot of people thought should be more generic. Hence the need for a generic persistence API.
Meanwhile, the JCP with a lot of input from Hibernate and other ORM tool vendors was developing JSR 220 (Java Specification Request) which resulted in JPA 1.0 (2006) and eventually JSR 317 which is JPA 2.0 (2009). These are specifications of a generic Java Persistence API. The API is provided in the JDK as a set of interfaces so that your classes can depend on the javax.persistence and not worry about the particular vendor that is doing the work of persisting your objects. This is only the API and not the implementation. Hibernate now becomes one of the many vendors that implement the JPA 2.0 specification. You can code toward JPA and pick whatever compliant ORM vendor suits your needs.
There are cases where Hibernate may give you features that are not codified in JPA. In this case, you can choose to insert a Hibernate specific annotation directly in your class since JPA does not provide the interface to do that thing.
Source: http://www.reddit.com/r/java/comments/16ovek/understanding_when_to_use_jpa_vs_hibernate/
JPA is the interface while Hibernate is the implementation.
Traditionally there have been multiple Java ORM solutions:
Hibernate
TopLink
JDO
each implementation defining its own mapping definition or client API. The JPA expert group gathered the best of all these tools and so they created the Java Persistence API standard.
A standard persistence API is very convenient from a client point of view, making it relatively easy to switch one implementation with the other (although in practice it's not that simple because on large projects you'll have to use specific non-standard features anyway).
The standard JPA has pushed Java ORM competition to a new level and this can only lead to better implementations.
As explained in my book, High-Performance Java Persistence, Hibernate offers features that are not yet supported by JPA:
extended identifier generators (hi/lo, pooled, pooled-lo)
transparent prepared statement batching
customizable CRUD (#SQLInsert, #SQLUpdate, #SQLDelete) statements
static or dynamic collection filters (e.g. #FilterDef, #Filter, #Where) and entity filters (e.g. #Where)
mapping properties to SQL fragments (e.g. #Formula)
immutable entities (e.g. #Immutable)
more flush modes (e.g. FlushMode.MANUAL, FlushMode.ALWAYS)
querying the second-level cache by the natural key of a given entity
entity-level cache concurrency strategies
(e.g. Cache(usage = CacheConcurrencyStrategy.READ_WRITE))
versioned bulk updates through HQL
exclude fields from optimistic locking check (e.g. #OptimisticLock(excluded = true))
versionless optimistic locking (e.g. OptimisticLockType.ALL, OptimisticLockType.DIRTY)
support for skipping (without waiting) pessimistic lock requests
support for Java 8 Date and Time
support for multitenancy
support for soft delete (e.g. #Where, #Filter)
These extra features allow Hibernate to address many persistence requirements demanded by large enterprise applications.
From the Wiki.
Motivation for creating the Java Persistence API
Many enterprise Java developers use lightweight persistent objects provided by open-source frameworks or Data Access Objects instead of entity beans: entity beans and enterprise beans had a reputation of being too heavyweight and complicated, and one could only use them in Java EE application servers. Many of the features of the third-party persistence frameworks were incorporated into the Java Persistence API, and as of 2006 projects like Hibernate (version 3.2) and Open-Source Version TopLink Essentials have become implementations of the Java Persistence API.
As told in the JCP page the Eclipse link is the Reference Implementation for JPA. Have look at this answer for bit more on this.
JPA itself has features that will make up for a standard ORM framework. Since JPA is a part of Java EE spec, you can use JPA alone in a project and it should work with any Java EE compatible Servers. Yes, these servers will have the implementations for the JPA spec.
Hibernate is the most popular ORM framework, once the JPA got introduced hibernate conforms to the JPA specifications. Apart from the basic set of specification that it should follow hibernate provides whole lot of additional stuff.
JPA is just a specification which needs concrete implementation.
The default implementation oracle provide is "Eclipselink" now. (Toplink is donated by Oracle to Eclipse foundation to merge with eclipselink)
(Reference : http://www.oracle.com/technetwork/middleware/toplink/index-085257.html
http://www.eclipse.org/org/press-release/20080317_Eclipselink.php
)
Using Eclipselink, one can be sure that the code is portable to any implementation if need arises.
Hibernate is also a full JPA implementation + MORE ( Sort of JPA Plus). Hibernate is super set of JPA with some extra Hibernate specific functionality.
So app developed in Hibernate may not be compatible when switched to other implementation.
Still hibernate is choice of majority of developers as JPA implementation and widely used.
Another JPA implementation is OpenJPA (openjpa.apache.org) which is an extension of Kodo implementation.
JPA : is just like an interface and have no concrete implementation of it to use functions which are there in JPA.
Hibernate : is just a JPA Provider which have the implementation of the functions in JPA and can have some extra functions which might not be there in JPA.
TIP : you can use
*combo 1* : JPA + JPA Provider(Hibernate)
*combo 2* : only Hiberante which does not need any interface
Combo 1 : is used when you feel that your hibernate is not giving better performance and want to change JPA Provider that time you don't have to write your JPA once again. You can write another JPA Provider ... and can change as many times you can.
Combo 2 : is used very less as when you are not going change your JPA Provider at any cost.
Visit http://blog-tothought.rhcloud.com//post/2, where your complete confusion will get clear.
JPA is the interface, Hibernate is one implementation of that interface.
JPA is a specification to standardize ORM-APIs. Hibernate is a vendor of a JPA implementation. So if you use JPA with hibernate, you can use the standard JPA API, hibernate will be under the hood, offering some more non standard functions.
See http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html_single/ and http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/
JPA is just a specification.In market there are many vendors which implements JPA. Different types of vendors implement JPA in different way. so different types of vendors provide different functionality so choose proper vendor based on your requirements.
If you are using Hibernate or any other vendors instead of JPA than you can not easily move to hibernate to EclipseLink or OpenJPA to Hibernate.But If you using JPA than you just have to change provide in persistence XML file.So migration is easily possible in JPA.
JPA is an API, one which Hibernate implements.Hibernate predates JPA. Before JPA, you write native hibernate code to do your ORM. JPA is just the interface, so now you write JPA code and you need to find an implementation. Hibernate happens to be an implementation.
So your choices are this:
hibernate, toplink, etc...
The advantage to JPA is that it allows you to swap out your implementation if need be. The disadvantage is that the native hibernate/toplink/etc... API may offer functionality that the JPA specification doesn't support.
While JPA is the specification, Hibernate is the implementation provider that follows the rules dictated in the specification.
Java - its independence is not only from the operating system, but also from the vendor.
Therefore, you should be able to deploy your application on different application servers.
JPA is implemented in any Java EE- compliant application server and it allows to swap application servers, but then the implementation is also changing. A Hibernate application may be easier to deploy on a different application server.
JPA is a specification that you implement in your data layer to perform db opertations, OR mappings and other required tasks.
Since it is just a specification, you need a tool to have it implemented. That tool can be either Hibernate, TopLink, iBatis, spring-data etc.
You don't necessarily require JPA if you are using Hibernate in your Data Layer. But if you use JPA specification for Hibernate, then it will make switching to other ORM tools like iBatis, TopLink easy in future, because the specification is common for others as well.
*(if you remember, you do import javax.persistence.*; when you use annotations for OR mapping (like #Id, #Column, #GeneratedValue etc.) in Hibernate, that's where you are using JPA under Hibernate, you can use JPA's #Query & other features as well)
JPA is a Java API specification which describes the management of relational data in applications using Java Platform. where as Hibernate is a ORM (Object Relational Mapping) library which follows JPA specification.
You can think JPA as a set of Rules which is implemented by Hibernate.
JPA is JSR i.e. Java Specification Requirement to implement Object Relational Mapping which has got no specific code for its implementation. It defines certain set of rules for for accessing, persisting and managing the data between Java objects and the relational databaseWith its introduction, EJB was replaced as It was criticized for being heavyweight by the Java developer community.
Hibernate is one of the way JPA can be implemented using te guidelines.Hibernate is a high-performance Object/Relational persistence and query service which is licensed under the open source GNU Lesser General Public License (LGPL) .The benefit of this is that you can swap out Hibernate's implementation of JPA for another implementation of the JPA specification. When you use straight Hibernate you are locking into the implementation because other ORMs may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.
JPA is just a specification which needs concrete implementation. The default implementation provided by oracle is "Eclipselink" now. Toplink is donated by Oracle to Eclipse foundation to merge with eclipselink.
Using Eclipselink, one can be sure that the code is portable to any implementation if need arises. Hibernate is also a full JPA implementation + MORE. Hibernate is super set of JPA with some extra Hibernate specific functionality. So application developed in Hibernate may not be compatible when switched to other implementation. Still hibernate is choice of majority of developers as JPA implementation and widely used.
Another JPA implementation is OpenJPA, which is an extension of Kodo implementation.
JPA vs Hibernate
I try to explain in very easy words.
Suppose you need a car as we all know their are several A class manufacturer like MERCEDES, BMW , AUDI etc.
Now in above statement CAR(is a specification) as every car have common features like thing with 4 wheels and can be driven on road is car...so its like JPA.
And MERCEDES, BMW , AUDI etc are just using common car feature and adding functionality according to their customer base so they are implementing the car specification like hibernate , iBATIS etc.
So by this common features goes to jpa and hibernate is just an implementation according to their jboss need.
1 more thing
JPA includes some basic properties so in future if you want to change hibernate to any other implementation you can easily switch without much headache and for those basic properties includes JPA annotations which can work for any implementation technology, JPQL queries.
So mainly we implement hibernate with JPA type technology just for in case we want to switch our implementation according to client need plus you will write less code as some common features are involved in JPA.
If someone still not clear then you can comment as i m new on stack overflow.
Thank you
JPA is just a specification while Hibernate is one of the JPA provider i.e hibernate is implementing various things mentioned in JPA contract.
JPA or Java Persistence API is a standard specification for ORM implementations whereas Hibernate is the actual ORM implementation or framework.
JPA is Java Persistence API. Which Specifies only the specifications for APIs. Means that the set of rules and guidelines for creating the APIs. If says another context, It is set of standards which provides the wrapper for creating those APIs , can be use for accessing entity object from database. JPA is provided by oracle.When we are going to do database access , we definitely needs its implementation. Means JPA specifies only guidelines for implementing APIs. Hibernate is a JPA provider/Vendor who responsible for implementing that APIs. Like Hibernate TopLink and Open JPA is some of examples of JPA API providers. So we uses JPA specified standard APIs through hibernate.
Figuratively speaking JPA is just interface, Hibernate/TopLink - class (i.e. interface implementation).
You must have interface implementation to use interface. But you can use class through interface, i.e. Use Hibernate through JPA API or you can use implementation directly, i.e. use Hibernate directly, not through pure JPA API.
Good book about JPA is "High-Performance Java Persistence" of Vlad Mihalcea.
To keep it short and sweet:
There is hibernate-commons-annotations 4.1.0-Final and hibernate-annotations 3.5.6-Final.
I'm a nub, what's the difference between them, and do I need them both?
Trying to "avoid" JPA and by that I mean using the JPA 2.0 standards embedded within Hibernate.
Thanks!
Previously, hibernate-annotations was released and versioned from hibernate core. But from version 3.5 and up it is included with hibernate core. And for some reason it was still released from 3.5.0 to 3.5.6 but you do not need it anymore.
And coming to hibernate-commons-annotations, it is a utility project used by annotations based hibernate sub-projects. It is used by other hibernate projects like hibernate-search and thus is maintained as a separate project and it is a compile time dependency for hibernate-core v3.6.0 and up.
Source 1
Source 2
Hibernate Commons Annotations is "Utility project for annotation handling", as said for example here. It does not contain such API that normal user of Hibernate should use.
Hibernate annotations contained persistence mapping annotations and related code. Nowadays it is merged to Hibernate core.
If you really want to avoid JPA (1/2) that is easily done by not using classes from javax.persistence package or from its subpackages. If you want opposite, use javax.persistence and avoid org.hibernate packages where possible.
Good guide to get started with Hibernate can be found from http://docs.jboss.org/hibernate/orm/4.1/quickstart/en-US/html_single/. It also tells which libraries are needed always and which ones are optional.
Reference documentation contains plenty of advices about using JPA instead of deprecated legacy Hibernate annotations.
AFAIK, the latest and greatest that I have been using is JPA 2.0.
I am befuddled that GAE has a jar somewhat called appengine-java-sdk-1.6.5/lib/user/orm/geronimo-jpa_3.0_spec-1.1.1.jar. I have been having the impression that GAE always has a latency in adopting compatibility with latest, greatest bleeding edge protocols. It took them some time to adopt JPA 2.0.
What is that JPA 3.0 jar doing inside GAE SDK libs? Is there such a version as JPA 3.0? Is Google one-upping us this time by implementing a new version protocol ahead of everyone else? Is there a JPA 3.0 spec or reference impl somewhere I could refer to?
I am not (currently) an EJB person (sorry ... had decided to avoid EJB ever since I had bad experience with EJB 1.0 10 years ago).
Is EJB 3.0 synonymous with JPA 2.0,
or is JPA 2.0 adopted as a subset of EJB 3.0
or is JPA 3.0 = EJB 3.0?
WRT this question I asked: moving from Hibernate entitymanager 3.6.9 to 4.1.2 crashed mysql connection,
Is Hibernate entity-manager 4.x somehow related to JPA 3.0 or at least a JPA version later than 2.0? I am experiencing significant differences between Hibernate entity-manager 3.x and 4.x (and have so far avoided deploying 4.x).
I realise this question is rather self-confusing and may be I should have asked each of the items as individual question, but I am hoping some one could tie all these together in a single thesis: ejb 3, JPA 2/3, geronimo, hibernate ent-mgr 3/4. Thanks.
No there is not such a thing (in June 2012) as JPA3. There is JPA1, JPA2, and (in planning) JPA2.1. That geronimo-specs jar is actually for JPA1 but some very shortsighted person/group decided to name it like that, and are now seeing the consequences of it.
As you say GAE already does support JPA2, using v2.x of their JPA plugin as available from Maven repositories or here http://code.google.com/p/datanucleus-appengine/ and that works with a compatible jpa-api v2 jar (such as geronimo-jpa_2.0_spec).
As others have said, JPA 2.0 is the current version (part of Java EE 6) and the next version is going to be JPA 2.1 which is currently in specification (JSR 338) and going to be included in Java EE 7. There is no JPA 3 as of yet, so that JAR is just badly named.
JPA 1.0 was introduced as part of Java EE 5, splitting the persistence part from the EJB specification (so Java EE 5 introduced EJB 3.0 and JPA 1.0, no more Entity Beans, yeah!). Java EE 6 then included EJB 3.1 and JPA 2.0.
Java EE 7 will include EJB 3.2 and JPA 2.1.
I am speaking under correction here as things might have changed since I last seriously worked with JPA. However JPA 2 from my understanding is the current specification. I doubt the jar you mentioned is a JPA 2 specification. In 2011 Geronimo did not support JPA 2 out of the box at that point.
As far as EJB's and JPA is concerned:
Both EJB and JPA are based on POJO's (Plain Old Java Objects).
JPA classes is essentially a data access layer technology i.e. used to interact with your data layer.
EJB is essentially used for a business layer. It would contain business logic and orchestrate interaction between different JPA classes. For example in JPA you might have a Customer Class (which interacts with the Customer Table) and a Orders Class (which interacts with the Orders table). In your EJB you will use both these classes to create a new order for a particular customer.
When using JPA as a technology it allows you to keep your database access funtionallity in one layer. This layer could be used by EJB's, a web application(Servlets,JSP,JSF), web services and even a swing application. This will keep the database access logic consistent across all these applications. Changes to the database will then require a change to the JPA.
This is a very simplified explanation of the differences and this subject matter is really deep but these points should help you differentiate between the two.
By the way the new EJB specification really makes beans very easy to use, develop and deploy. I am lucky I missed the original specification all those years ago. Currently they are very easy to use.
Hope this helps clear up the mist a bit.
I'm wanting using JPA in ear project. Development project must be started ASAP so I have not a lot of time to research and investigate. Could you say please JPA API is restricted functionality of Hibernate or no. At this moment I'm using Hibernate directly. For example in future I'm planing to use hibernate-search and maybe hibernare-validate and -shard. Can I be sure that in future I will not have problem with using this.
And one more example - can I use HAR archive and JPA together.
Why JPA? For project will available RESTful service (jersey or resteassy implementation). And as I looked in much case using JPA for this. I'm a newbie in this so it's only my IMHO. May be i mistakes.
Thanks a lot.
Best regards
Artem
JPA is a subset of hibernate, but you're not limited to it. If you need a hibernate specific feature, you can generally use it at the cost of being tied to hibernate. For example, we've mixed in hibernate annotations with JPA ones, including the validater ones, without trouble.
JPA in theory lets you change the persistance provider later.
Sticking to only JPA compatible configuration can cause more trouble that is solved by the dubious promise of seamlessly swapping providers however.
This morning I was aboard a S-Bahn (German Subway) and I met a fellow student, who works for IBM. What he is doing there is Java EE optimization. I told him about my little project. And he recommended not to use 'oldschool' Hibernate. That's why my question is:
Is Hibernate deprecated?
(In combination with Java EE/Web Development)
..or did he just prate..
No, Hibernate is not deprecated.
However, there's now JPA (Java Persistence API), which is a standard API for doing the things that Hibernate does.
Note that JPA is just an interface specification. You'll need something that implements JPA, and Hibernate is one of the implementations of JPA. Besides Hibernate, there are a few others such as EclipseLink (the official reference implementation for JPA) and Apache OpenJPA.
Hibernate, apart from being an implementation of JPA, does provide a lot of extra advanced feature that JPA lacks of (extra syntax in query, QBC support etc). Some of them are really useful and hard to find a workaround in JPA world (yet). Without providing such features, it is hard to say JPA can "replace" Hibernate (hence, saying Hibernate being deprecating)
Hibernate is the JPA provider offered by JBoss, which is a Java EE server, so I doubt that Hibernate as an implementation is deprecated.
Perhaps he meant that using Hibernate within a Java EE server , bypassing the container-provider persistence, is deprecated and you should rely on our container for such services.
No, there is no way that Hibernate is deprecated. There is the JPA which is a persistence specification and Hibernate implements it. Also Hibernate has its own advanced features that JPA does not have and that's why Hibernate is the main source of new features that are added to the JPA standard.
One possible reason why he may have suggested you against Hibernate is that for a small project, the overhead of understanding Hibernate can be quite significant.
Hibernate is vast to say the least. Though it can be used in a simple way, but to find that out too, you'll need to comprehend a whole lot more.
but be rest assured that Hibernate is NOT deprecated, or going to be any time in the distant future. it's just that if your ORM needs are modest, you might want to try other solutions like iBATIS
JPA is only one way to do it. There's still Spring and all the other frameworks where Hibernate is well alive.
Based on what you said, it sounds like he may have been referring to Hibernate xml mappings, in contrast to using Hibernate annotations or JPA. XML is most certainly old-school rubbish.