简体   繁体   English

Hibernate单向@ManyToMany:删除没有约束冲突吗?

[英]Hibernate unidirectional @ManyToMany : delete without constraint violations?

I have a class A : 我有A班:

@Entity
public class A {
  ...
  @ManyToMany
  private Set<Ref> refs = new HashSet<Ref>();
  ...
}

And a class Ref : 和类Ref:

@Entity
public class Ref {
 // no link to A
}

I want to delete A and A_Ref but not Ref, but i get a org.hibernate.exception.ConstraintViolationException 我想删除A和A_Ref但不删除Ref,但是我得到了org.hibernate.exception.ConstraintViolationException

Is there a simple way to do it or should i explicitely create a A_Ref class ? 有没有简单的方法可以做到这一点,还是我应该明确创建A_Ref类?

Thanks 谢谢

Edit : I was attempting to delete my list of A directly in hql. 编辑:我试图直接在hql中删除我的A列表。 I did it in object (broke the relations) and it worked (cascade + orphan deletion). 我这样做的目的(打破关系),它的工作(级联+孤儿删除)。

I am not sure, if I got you right, but from what I understood my first guess is, that you havent used all necessary annotations to make sure, that hibernate will automatically remove necessary references in Ref by itself. 我不确定,如果我做对了,但是据我了解,我的第一个猜测是,您没有使用所有必要的注释来确保休眠状态将自动自动删除Ref中的必要引用。

Within a manyToMany relation, you can use joinTables and cascadetypes to make sure, that hibernate knows where to delete all necessary relations by itself without creating an own domainObject for it. 在manyToMany关系中,可以使用joinTables和cascadetypes来确保休眠知道自己在哪里删除所有必要的关系,而无需为其创建自己的domainObject。

There are plenty of nice guides how to manage it. 有很多不错的指南来介绍如何进行管理。 On first view the guide from mkyong looks pretty good. 乍看之下mkyong的指南看起来不错。 Note: He annotated getters instead of variable declarations! 注意:他注释了getter而不是变量声明! (which is just a question of taste). (这只是口味问题)。

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

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