similarity and difference between jpa and hibernate
what开发者_开发知识库 is similarity and difference between jpa and hibernate.
JPA (Java Persistence API) is an interface for persistence providers to implement. Hibernate is one such implementation of JPA.
This is the introduction of the JSR-000220 Enterprise JavaBeans 3.0 Final Release (persistence):
This document is the specification of the Java API for the management of persistence and object/rela- tional mapping with Java EE and Java SE. The technical objective of this work is to provide an object/relational mapping facility for the Java application developer using a Java domain model to man- age a relational database.
This persistence API—together with the query language and object/relational mapping metadata defined in this document—is required to be supported under Enterprise JavaBeans 3.0. It is also targeted at being used stand-alone with Java SE.
Leading experts throughout the entire Java community have come together to build this Java persistence standard. This work incorporates contributions from the Hibernate, TopLink, and JDO communities, as well as from the EJB community.
In other words, JPA is the standardized API for persistence. Hibernate provides an implementation of the standard (i.e. it can be used as underlying persistence engine when using JPA).
Prior to JPA, Hibernate was a kind of de facto standard for object persistence in Java. Hibernate is considered as a major contributor to JPA so there are lots of similarities between them (I could even say between all ORM solutions as ORM concepts are common). However, for various reasons, political or technical, the JPA working group couldn't include everything in the first release of JPA so JPA is actually a subset of Hibernate (this is a simplified view but the reality is very close). Hibernate and other JPA implementation provide thus their own proprietary extensions (which means non standards i.e. not portable from one provider to the other) to the JPA standard that you may use, or not.
As pointed out by @Pascal Hibernate existed prior to JPA standard(it is now JSR 317 JPA 2.0, which Hibernate has implemented in 3.5 already out for early use). So there are other providers of JPA e.g. Oracle TopLink, Apache OpenJPA. To use strictly JPA in Hibernate you have to use EntityManager
as apposed to the Session
which is a Hibernate concept pre-dating JPA. Similarly for strict JPA you have to use EntityManagerFactory
as apposed to SessionFactory
.
The EntityManager
and EntityManagerFactory
are, in fact, thin wrappers around the Session
and SessionFactory
respectively. In addition, core/annotations Hibernate provide extensions to the JPA spec that make your life easier. Before JPA 2 Hibernate had a Criteria API which allowed you to programmatically construct a query in an OO fashion. JPA 2 now makes this functionality standard -- the JPA standard is a bit different from the Hibernate version as it employs generics. So basically the Hibernate functionality is a superset of JPA.
Here's a list of some difference between subjects (excerpts from Hibernate documentation)
Entity. Hibernate goes beyond the JPA specification and provide additional configurations. Some of them are hosted on @org.hibernate.annotations.Entity
- dynamicInsert / dynamicUpdate (defaults to false)
- selectBeforeUpdate (defaults to false)
- polymorphisms
- persister
- optimisticLock (version, all, dirty, none)
id as a property using a component type
- While not supported in JPA, Hibernate lets you place your association directly in the embedded id component (instead of having to use the @MapsId annotation)
Multiple id properties without identifier type
- Another, arguably more natural, approach is to place @Id on multiple properties of your entity. This approach is only supported by Hibernate (not JPA compliant) but does not require an extra embeddable component.
Multiple id properties with with a dedicated identifier type
- While not JPA standard, Hibernate let's you declare the vanilla associated property in the @IdClass
Identifier generator
- Package level definition is not supported by the JPA specification. However, you can use the @GenericGenerator at the package level
Annotations
- Hibernate Annotations supports something that is not explicitly supported by the JPA specification. You can annotate a embedded object with the @MappedSuperclass annotation to make the superclass properties persistent (see @MappedSuperclass for more informations).
Discriminator
- @org.hibernate.annotations.DiscriminatorOptions allows to optionally specify Hibernate specific discriminator options which are not standardized in JPA. The available options are force and insert
Transaction
- Hibernate provides more flush modes than the one described in the JPA specification. In particularFlushMode.MANUAL for long running conversation. Please refer to the Hibernate core reference documentation for more informations.
Static metamodel
- Important. As of today the JPA 2 metamodel does not provide any facility for accessing relational information pertaining to the physical model. It is expected this will be addressed in a future release of the specification.
精彩评论