org.hibernate.annotations vs. javax.persistence

JavaHibernateJpa

Java Problem Overview


Is it a bad idea to use the annotations from the

> javax.persistence package

instead of using the

> org.hibernate.annotations annotations

I know that using javax.peristence does introduce yet another dependency. But if I ignore that, what are the pros/cons?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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!

Solution 4 - Java

Officially recommended to mix JPA and Hibernate annotations in the case of setting cascading options, see [Hibernate docs. 2.4.7. Cascade][1]. If you using only JPA annotations, in case of unidirectional mapping (no field of Foo type in Employer.java) you still getting "cannot save transient object Employer" in the call session.SaveOrUpdate. Cure is using hibernate-style @Cascade together with cascade={...}:

class Foo {
     @OneToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
     @Cascade(org.hibernate.annotations.CascadeType.REPLICATE)
     public Collection<Employer> getEmployers()
...
}

[1]: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-hibspec-cascade "Hibernate docs. 2.4.7. Cascade"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionanonView Question on Stackoverflow
Solution 1 - JavaThiloView Answer on Stackoverflow
Solution 2 - JavagavenkoaView Answer on Stackoverflow
Solution 3 - JavaMr ColdView Answer on Stackoverflow
Solution 4 - JavaLordView Answer on Stackoverflow