简体   繁体   English

org.hibernate.annotations 与 javax.persistence

[英]org.hibernate.annotations vs. javax.persistence

Is it a bad idea to use the annotations from the使用来自

javax.persistence package javax.persistence package

instead of using the而不是使用

org.hibernate.annotations annotations org.hibernate.annotations 注释

I know that using javax.peristence does introduce yet another dependency.我知道使用javax.peristence确实引入了另一个依赖项。 But if I ignore that, what are the pros/cons?但如果我忽略这一点,有什么优点/缺点?

Quite the oppsite.恰恰相反。

Hibernate is an implementation of the Java Persistence API, and where possible, you should use the standard annotations (in javax.persistence). Hibernate 是 Java 持久性 API 的实现,并且在可能的情况下,您应该使用标准注释(在 javax.persistence 中)。 This way, you could theoretically run your code on other JPA implementations.这样,理论上您可以在其他 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 文件和非常轻。

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 @OneToMany is from JPA, it expects a JPA cascade – javax.persistence.CascadeType .将不起作用,因为@OneToMany来自 JPA,它需要一个 JPA 级联 - javax.persistence.CascadeType However when you save it with Hibernate session, org.hibernate.engine.Cascade will do the following check:但是,当您使用 Hibernate session 保存它时, org.hibernate.engine.Cascade将执行以下检查:

if ( style.doCascade( action ) ) {

and Hibernate save process will causing a ACTION_SAVE_UPDATE action, but the JPA will pass a ACTION_PERSIST and ACTION_MERGE , it will not match and cause the cascade to fail to execute.和 Hibernate 保存过程将导致ACTION_SAVE_UPDATE动作,但 JPA 将传递ACTION_PERSISTACTION_MERGE ,它将不匹配并导致级联无法执行。

I used javax.persistence annotation, and when I replaced Tomcat 6.0 with my Glass Fish, then Tomcat 6.0 included another javax.persistence package that messed everything.我使用了javax.persistence注释,当我用我的 Glass Fish 替换 Tomcat 6.0 时,Tomcat 6.0 包含另一个 javax.persistence ZEFE90A8E604A7C840E88D03AD87。 I don't think it's a good idea to use javax.persistence annotation.我认为使用javax.persistence注释不是一个好主意。 God know what the hell happened with Tomcat and javax.persistence !天知道 Tomcat 和javax.persistence到底发生了什么!

Officially recommended to mix JPA and Hibernate annotations in the case of setting cascading options, see Hibernate docs.官方推荐在设置级联选项的情况下混合使用 JPA 和 Hibernate 注解,参见Hibernate 文档。 2.4.7. 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.如果您只使用 JPA 注释,在单向映射的情况下(在 Employer.java 中没有 Foo 类型的字段),您仍然会在调用 Z21D6F40CFB5121982E4424ESaveOrUpdate.5082E4424ESaveOrUpdate.5082E4424A00 Cure is using hibernate-style @Cascade together with cascade={...}: Cure 使用休眠风格的@Cascade 和 cascade={...}:

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

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

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