org.hibernate.annotations 与 javax.persistence

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/700558/
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 18:30:24  来源:igfitidea点击:

org.hibernate.annotations vs. javax.persistence

javahibernatejpa

提问by

Is it a bad idea to use the annotations from the

使用来自

javax.persistence package

javax.persistence 包

instead of using the

而不是使用

org.hibernate.annotations annotations

org.hibernate.annotations 注释

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

我知道 usingjavax.peristence确实引入了另一个依赖项。但如果我忽略这一点,利弊是什么?

采纳答案by Thilo

Quite the oppsite.

恰恰相反。

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.

Hibernate 是 Java Persistence API 的实现,如果可能,您应该使用标准注释(在 javax.persistence 中)。这样,理论上您可以在其他 JPA 实现上运行您的代码。

Only when you need Hibernate-specific functionality should you use the Hibernate annotations.

只有当您需要特定于 Hibernate 的功能时,才应该使用 Hibernate 注释。

The extra dependency is only on the JPA interface/annotation jar files and very light.

额外的依赖只依赖于 JPA 接口/注释 jar 文件并且非常轻。

回答by Mr Cold

I used javax.persistenceannotation, 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.persistenceannotation. God know what the hell happened with Tomcat and javax.persistence!

我使用了javax.persistence注解,当我用 Glass Fish 替换 Tomcat 6.0 时,Tomcat 6.0 包含了另一个 javax.persistence 包,它搞砸了一切。我认为使用javax.persistence注释不是一个好主意。天知道 Tomcat 和javax.persistence!

回答by gavenkoa

Another cons in:

另一个缺点:

http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/

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 @OneToManyis from JPA, it expects a JPA cascade – javax.persistence.CascadeType. However when you save it with Hibernate session, org.hibernate.engine.Cascadewill do the following check:

不会像@OneToManyJPA那样工作,它需要 JPA 级联 – javax.persistence.CascadeType. 但是,当您使用 Hibernate 会话保存它时,org.hibernate.engine.Cascade将进行以下检查:

if ( style.doCascade( action ) ) {

and Hibernate save process will causing a ACTION_SAVE_UPDATEaction, but the JPA will pass a ACTION_PERSISTand ACTION_MERGE, it will not match and cause the cascade to fail to execute.

和 Hibernate 保存过程会导致一个ACTION_SAVE_UPDATE动作,但 JPA 会传递一个ACTION_PERSISTand ACTION_MERGE,它不会匹配并导致级联无法执行。

回答by Lord

Officially recommended to mix JPA and Hibernate annotations in the case of setting cascading options, see Hibernate docs. 2.4.7. Cascade. 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={...}:

官方建议在设置级联选项的情况下混合使用 JPA 和 Hibernate 注释,请参阅 Hibernate 文档。2.4.7. 级联。如果您只使用 JPA 注释,在单向映射(Employer.java 中没有 Foo 类型的字段)的情况下,您仍然会在调用 session.SaveOrUpdate 中得到“无法保存瞬态对象 Employer”。Cure 使用 hibernate-style @Cascade 和 cascade={...} :

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