JPA实体和Hibernate实体之间的区别

时间:2009-06-05 11:59:51

标签: java hibernate jpa

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

javax包是JPA的实体注释,但为什么有一个hibernate实体注释,它与JPA的注释有什么区别?它只是一个允许定义更多属性的扩展吗?

3 个答案:

答案 0 :(得分:34)

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

<击> 来自the FAQ修改:新链接the specific question 修改:新链接the answer

  

我使用@ org.hibernate.annotations.Entity并获取未知实体异常

     

始终导入@ javax.persistence.Entity

     

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

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

像这样:

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

答案 1 :(得分:1)

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

之间的主要区别

答案 2 :(得分:-1)

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

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