开发者

alternative way for many-to-many relationship in jpa

How to write code with many-to-many relationship without using jpa @manytomany annotation? For example for two classes Software and for Tags. When i delete Tag - it removes all software. I tried to do it with @manytomany annotation using various approaches but it does not work. I have seen something similar to my question in this site but any gives the code sample. (I guess it should be @OneToMany relationships in both sides, but it would be great to take a code).

Or how to do it with using @manytomany if it possible.

More info: if we have soft1, soft2 <--> tag1, and soft2 <--> tag2, and we delete tag1, then only 开发者_如何学JAVAsoft1 should be deleted. Does hibernate can handle it?


try with

@org.hibernate.annotations.Cascade(
   {org.hibernate.annotations.CascadeType.PERSIST, 
    org.hibernate.annotations.CascadeType.MERGE, 
    org.hibernate.annotations.CascadeType.REFRESH, 
    org.hibernate.annotations.CascadeType.DETACH, 
    org.hibernate.annotations.CascadeType.DELETE_ORPHAN}
)

This is to remove org.hibernate.annotations.CascadeType.REMOVE from the cascade to avoid the following scenario

  • You delete Software with id:5
  • hibernate deletes Tags with id:5 and 7
  • ALL the rows of software tagged with those ids are deleted, and the cascade starts again, so you might end up with an empty DB.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜