Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.hibernate.annotations vs. javax.persistence

People also ask

Is javax persistence same as Hibernate?

Hibernate is an implementation of the Java Persistence API, and where possible, you should use the standard annotations (in javax. persistence). This way, you could theoretically run your code on other JPA implementations. Only when you need Hibernate-specific functionality should you use the Hibernate annotations.

Why we are using JPA annotation instead of Hibernate?

You use hibernate as implementation of JPA API. You should be able to change hibernate with another implementation (like EclipseLink) without changing in the code. This is why you should only use JPA annotations.

What is the benefit of using JPA over Hibernate API?

As you've seen, JPA provides an easy to use API to implement common CRUD use cases without writing any SQL. That makes the implementation of common use cases a lot faster, but it also provides another benefit: Your SQL statements are not spread all over your code.


Quite the opposite.

Hibernate is an implementation of the Java Persistence API, and where possible, you should use the standard annotations (in javax.persistence). This way, you could theoretically run your code on other JPA implementations.

Only when you need Hibernate-specific functionality should you use the Hibernate annotations.

The extra dependency is only on the JPA interface/annotation jar files and very light.


Another cons in:

http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/

where this:

@OneToMany(fetch = FetchType.LAZY, 
  cascade = {CascadeType.PERSIST,CascadeType.MERGE }, 
  mappedBy = "stock")
public Set<StockDailyRecord> getStockDailyRecords() {
    return this.stockDailyRecords;
}

with this:

stockDailyRecords.setStock(stock);        
stock.getStockDailyRecords().add(stockDailyRecords);

session.save(stock);
session.getTransaction().commit();

won't work as @OneToMany is from JPA, it expects a JPA cascade – javax.persistence.CascadeType. However when you save it with Hibernate session, org.hibernate.engine.Cascade will do the following check:

if ( style.doCascade( action ) ) {

and Hibernate save process will causing a ACTION_SAVE_UPDATE action, but the JPA will pass a ACTION_PERSIST and ACTION_MERGE, it will not match and cause the cascade to fail to execute.


I used javax.persistence annotation, and when I replaced Tomcat 6.0 with my Glass Fish, then Tomcat 6.0 included another javax.persistence package that messed everything. I don't think it's a good idea to use javax.persistence annotation. God know what the hell happened with Tomcat and javax.persistence!