Wednesday, May 14, 2014

javax.persistence vs. org.hibernate.annoatations


While working in DAO layer using Hibernate as an ORM tool, you must have come across these two packages to import the annotations in a POJO class. I'm going to explain which one to use and why.

The package javax.persistence is provided by Java Persistence API (JPA) whereas org.hibernate.annotations is provider by Hibernate. JPA is a specification which defines a set of rules, syntax and symantics to manage the relational data. It defines programming interfaces, lifecycle rules for persistent objects and query features. Whereas Hibernate is a real implementation of JPA. There are other JPA implementations as well such as TopLink, OpenJPA, DataNucleus, EclipseLink. Hibernate is supposed to be more powerful and use-voted selection though. I would recommend to go with javax.persistence. That's because if you wanted to switch to other implementations than Hibernate, you would be able to do that with no changes or very less changes in the POJO classes. This ensures the "loose coupling" design.

Moreover, Hibernate implementation its own enhancements or extra features. For instance, the fetching option ( @BatchSize ), the caching option ( @Cache) are available only in Hibernate. So, only when you need Hibernate-specific functionality, you should use the Hibernate annotations, otherwise, use JPA annotations.

References:


No comments :

Post a Comment