简体   繁体   中英

Hibernate unidirectional @ManyToMany : delete without constraint violations?

I have a class A :

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

And a class 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

Is there a simple way to do it or should i explicitely create a A_Ref class ?

Thanks

Edit : I was attempting to delete my list of A directly in hql. 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.

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.

There are plenty of nice guides how to manage it. On first view the guide from mkyong looks pretty good. Note: He annotated getters instead of variable declarations! (which is just a question of taste).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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