What does Hibernate/Toplink offer above JPA? - java

As far as I know, JPA itself offers all the shiny features like ORM, JPQL, entity relations mapping and so on. But I don't really understand, why do people use Hibernate or Toplink on top of JPA.
What does Hibernate offer that JPA itself doesn't have?

JPA is just a specification. Hibernate and TopLink are implementations of that specification.
Also, the JPA spec is a bit weak, it provides only a subset of the functions that the likes of Hibernate and TopLink provide. Sometimes it is desirable or necessary to go beyond JPA and use the proprietary features of the underlying implementation.

You should make your comparison against JPA1, and then against JPA2 (not yet final, but there is a spec), and look at the features you want in your application to see if any are not supported by JPA. This is the only sensible way of assessing whether you can use pure JPA. All implementations of persistence technology have their own add ons that may make a whole lot of sense to your application, but they may not too. Assess your application for the types of relations you have between objects. To get you started, do you use fields that are interfaces ? JPA has no support for interface-defined relations. JPA1 has no support for 1-N uni dir FK relations, but this is in JPA2.
--Andy (DataNucleus)

Related

What's the difference between JPA and Hibernate? [closed]

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.

Different mapping to database types with different JPA Implementations

I'm developing a Java EE Application that uses JPA to persist its data.
At first I used Hibernate as the JPA implementation, but later I switched to OpenJPA, because that's what the Weblogic Application Server here comes equipped with.
That didn't pose any problems, but it got me thinking:
Are there differences in the mapping from Java datatypes to the database specific datatypes for the various implementations of JPA? If yes what are they? Do I always have to think about these differences or are there any common pitfalls that are easy to avoid?
The JPA spec does not define the default mappings from Java type to JDBC type, as a result yes of course you have to think about this when designing your metadata. A good read of the spec would be recommended to see the concepts that it simply doesn't tackle.
The JDO spec defines these default mappings FWIW, so this aspect of portability there is not a problem.

Hibernate or JPA?

With Spring 3.0 is it recommended to use hibernate or JPA.What are the advantages and disadvantages of both of them when used with Spring 3.0 ?
Note : We are also suppose to use it with Spring Data Access.
I would say, JPA implementation provided by Hibernate. Why? Because
It would be easier to switch to some other JPA implementation later, if you you ever need to
Hibernate is the one of the major and most popular ORM around
Lot of books available
Extensive documentation, awesome reference material
Easy to get support on SO, and elsewhere
Actually, I don't mind to use Hibernate exclusive features too. I don't see any problem in sticking with Hibernate, just because its not an standard. And by the way, what makes you think Spring is standard. Its not, and you are fine with it, because it works. Similar thing can be said for Hibernate. Hibernate sometimes get hairy, if you don't know well what you are doing.
My preference is JPA with EclipseLink. Reasons:
JPA is standard, Hibernate is not
Use EclipseLink because JPA with Hibernate has some weird implementation. EclipseLink is also the reference implementation for JPA 2.0
Bozho: there are some that I found, unfortunately it is not so obvious because it only happens in some extreme cases. Some that I can think:
Convert JQL to CriteriaBuilder
ElementCollection works fine with JoinTable, which is wrong! ElementCollection should be accompanied by CollectionTable.
To be precise, you should use the Java Persistence API, and then you can use Hibernate as an implementation.
JPA without an implementation isn't worth anything.
JPA is a standard, so if you only use it's API, you can substitute Hibernate with some other JPA implementation. If you use some of Hibernate's specific API because you need functionality not provided with plain JPA, you'll have a vendor lock-in scenario, so you'll have to be careful about this.
It depends on our application.
If you are not sure that you will never need to change your persistence provider, it is strongly recommended to use JPA (2.0)
If you are sure that you never will change your persistence provider, than I recommend using JPA (2.0) too. But (only) in this case you have the possibility to use a proprietary feature of you choosen JPA-Provider, if you have a problem not solved by standard JPA. -- But use it wise, if you choose this way once, it is hard to go back!
Of course JPA is just an API - so you need an implementation of it. -- I used Hibernate as JPA provider, but mainly because I am allways in the second scenario (will never change it), and I needed additional features like Hibernate-Search and Envers.

JDO integration with hibernate

in JPA, to use hibernate, the only thing need to do is moodify persitence.xml and add in hibernate configuration. May i know with JDO, can just by modifying jdoconfig.xml, able to integrate with hibernate? any reference or example on this?
No, the reason it works on hibernate+JPA is the JPA specification was developed to be "compatible" with hibernate since hibernate is the dominant persistence API. The tech leads wished that people could move to JPA from hibernate easily.
Furthermore, the JPA specification is not as rigorous or detailed as JDO and thus it is a little more onerous to support JDO (This applies on JPA 1.0, don't know if later versions of JPA bring it more into line with JDO).
The JDO specification was developed independently of hibernate and thus cannot be switched onto JDO.
One obvious difference between hibernate/JPA and JDO is that JDO does not support annotations (it's a pre java 5 specification).
You can explore this through ObJectRelationalBridge:
http://db.apache.org/ojb/docu/tutorials/jdo-tutorial.html
Also, I am sure hibernate shall support JDO once JDO gets accepted as the primary way to do things.
JDO is Object Abstraction and it can be supported on any kind of database, however its implementation on Object Data Bases, it is more natural. Google App engine supports JDO since many years.

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.

Categories