简体   繁体   中英

Hibernate or JPA annotations to use

I am using Hibernate in our projects and annotation based configuration for Hibernate Domain Pojo Objects. For Annotations based configuration we have two options

  1. JPA based annotations using javax.persistence.*
  2. Use Hibernate Native Annotations org.hibernate.annotations.*

Currently we use JPA based annotation configuration for our POJO files and Hibernate native API like SessionFactory, Session, etc to open session and perform DB operations.

I have these questions:

  1. Is there any problem mixing both JPA annotations and use Hibernate native API?
  2. This link explains one such issue (cascade-jpa-hibernate-annotation-common-mistake)
  3. Please provide your expertise, which type of annotations to use
    1. JPA
    2. Hibernate native
    3. Mix both of them?

Hibernate provides one of the JPA implementations. If you use purely JPA in your code, you are free to change to a different implementation if a requirement arises. For example, EclipseLink/TopLink and OpenJPA are implementations which may be required for a different customer. A comprehensive list of implementations is here .

If you are compelled to use any exotic features provided by hibernate which are not in JPA specification, you should go for hibernate specific APIs in your code base. A related discussion from hibernate forum here .

We mixed some of these annotations since with the version of hibernate we were working those days, some features were not available on the JPA spec at that time. For instance to store a boolean value as a 'Y' or 'N' on th DB you have hibernate types you can use. But no such feature was available with the JPA spec at that time. I do not know about the status now. Also for orphan removal also those days JPA did not have the feature, but now i belive they provide an attribute called orphanRemoval on your cascade options. Also you have certain features such as @BatchSize to increase fetching performance with respect to bags. I am unaware if such features are available on the JPA spec yet.

In my experience, mixing and matching plus points from both would be beneficial given that you have no need of switching from one ORM to another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM