简体   繁体   English

要使用的Hibernate或JPA注释

[英]Hibernate or JPA annotations to use

I am using Hibernate in our projects and annotation based configuration for Hibernate Domain Pojo Objects. 我在Hibernate Domain Pojo Objects的项目和基于注释的配置中使用Hibernate。 For Annotations based configuration we have two options 对于基于注释的配置,我们有两个选项

  1. JPA based annotations using javax.persistence.* 使用javax.persistence.*基于JPA的注释javax.persistence.*
  2. Use Hibernate Native Annotations org.hibernate.annotations.* 使用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. 目前,我们为POJO文件和Hibernate本机API(如SessionFactory,Session等)使用基于JPA的注释配置来打开会话并执行数据库操作。

I have these questions: 我有这些问题:

  1. Is there any problem mixing both JPA annotations and use Hibernate native API? 混合JPA注释和使用Hibernate本机API有什么问题吗?
  2. This link explains one such issue (cascade-jpa-hibernate-annotation-common-mistake) 链接解释了一个这样的问题(cascade-jpa-hibernate-annotation-common-wrong)
  3. Please provide your expertise, which type of annotations to use 请提供您的专业知识,使用哪种注释
    1. JPA JPA
    2. Hibernate native Hibernate原生
    3. Mix both of them? 混合他们两个?

Hibernate provides one of the JPA implementations. Hibernate提供了一个JPA实现。 If you use purely JPA in your code, you are free to change to a different implementation if a requirement arises. 如果您在代码中使用纯粹的JPA,则可以在需要时自由更改为其他实现。 For example, EclipseLink/TopLink and OpenJPA are implementations which may be required for a different customer. 例如, EclipseLink / TopLinkOpenJPA是不同客户可能需要的实现。 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. 如果您被迫使用hibernate提供的任何不在JPA规范中的异乎寻常的功能,您应该在代码库中使用特定于hibernate的API。 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. 我们混合了其中一些注释,因为当时我们正在使用hibernate的版本,当时JPA规范上没有一些功能。 For instance to store a boolean value as a 'Y' or 'N' on th DB you have hibernate types you can use. 例如,要将一个布尔值存储为数据库中的“Y”或“N”,您可以使用休眠类型。 But no such feature was available with the JPA spec at that time. 但当时JPA规范没有提供此类功能。 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. 同样对于孤儿删除也是那些日子JPA没有这个功能,但现在我相信他们在你的级联选项上提供了一个名为orphanRemoval的属性。 Also you have certain features such as @BatchSize to increase fetching performance with respect to bags. 此外,您还拥有某些功能,例如@BatchSize,以提高相关行李的抓取性能。 I am unaware if such features are available on the JPA spec yet. 我不知道JPA规范上是否有这样的功能。

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. 根据我的经验,混合和匹配以及两者的加分都是有益的,因为您无需从一个ORM切换到另一个ORM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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