Java JPA实体和Hibernate实体的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/955515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:33:16  来源:igfitidea点击:

Difference between JPA Entity and Hibernate Entity

javahibernatejpa

提问by Kim L

When I annotate a class with @Entity and try to resolve the dependencies, I get to choose the package between two different packages, javax.persistence.Entity and org.hibernate.annotations.Entity

当我使用 @Entity 注释一个类并尝试解决依赖关系时,我可以在两个不同的包 javax.persistence.Entity 和 org.hibernate.annotations.Entity 之间选择包

The javax package is JPA's entity-annotation, but why is there a hibernate entity-annotation and difference does it have with JPA's annotation? Is it just an extension to allow more attributes to be defined?

javax包是JPA的entity-annotation,但是为什么会有hibernate entity-annotation,和JPA的annotation有区别吗?它只是允许定义更多属性的扩展吗?

采纳答案by H Marcelo Morales

org.hibernate.annotations.Entityhas some extra attributes that javax.persistence.Entityhas not standarized. The extra features will only work if using hibernate's AnnotationConfigurationdirectly or if hibernate is the JPA provider.

org.hibernate.annotations.Entity具有一些javax.persistence.Entity尚未标准化的额外属性。额外的功能只有在AnnotationConfiguration直接使用 hibernate或者 hibernate 是 JPA 提供者时才有效。

from the FAQ: edit:new link the specific question: edit:new link the answer:

来自常见问题解答编辑:新链接特定问题编辑:新链接答案

I use @org.hibernate.annotations.Entity and get an Unknown entity exception

Always import @javax.persistence.Entity

@org.hibernate.annotations.Entity completes @javax.persistence.Entity but is not a replacement

我使用@org.hibernate.annotations.Entity 并得到一个未知实体异常

始终导入@javax.persistence.Entity

@org.hibernate.annotations.Entity 完成 @javax.persistence.Entity 但不是替代品

For instance, there is an attribute called optimisticLock, which tells hibernate whether to use the standard version columnor to compare all columns when updating. This behavior is not in the JPA spec, so in order to configure it, you must use hibernate specific extension found in their own annotation.

例如,有一个名为 的属性optimisticLock,它告诉 hibernate在更新时是使用标准版本列还是比较所有列。此行为不在 JPA 规范中,因此为了配置它,您必须使用在它们自己的注释中找到的特定于休眠的扩展。

Like this:

像这样:

@Entity
@org.hibernate.annotations.Entity(optimisticLock=OptimisticLockType.ALL)
public class MyEntity implements Serializable {
...
}

回答by Peter D

I'm not sure about the differences but I am sure that if you have the Hibernate jars in your classpath you are using Hibernate JPA. Hibernate provides an implementation of JPA. Even though you are using the javax.persistence package you are using Hibernate JPA.

我不确定这些差异,但我确信如果您的类路径中有 Hibernate jars,那么您使用的是 Hibernate JPA。Hibernate 提供了 JPA 的实现。尽管您使用的是 javax.persistence 包,但您使用的是 Hibernate JPA。

The difference could be only in the naming. They might provide the same classes both in the Hibernate package space and the javax package space.

区别可能仅在于命名。它们可能在 Hibernate 包空间和 javax 包空间中提供相同的类。

回答by Ganesamoorthi

@org.hibernate.annotations used in your project, if suppose you want to use JDBC template or ibatis we need to change the code. if we use javax.persistence there is no need to change the code. This is the main difference between org.hibernate.annotations and javax persistence

@org.hibernate.annotations 在你的项目中使用,如果你想使用 JDBC 模板或 ibatis 我们需要更改代码。如果我们使用 javax.persistence 则无需更改代码。这是 org.hibernate.annotations 和 javax 持久性之间的主要区别