Spring JPA 并从 @ManyToMany 中删除条目

Posted

技术标签:

【中文标题】Spring JPA 并从 @ManyToMany 中删除条目【英文标题】:Spring JPA and remove entries from @ManyToMany 【发布时间】:2013-04-05 17:51:45 【问题描述】:

如下所述:

Cascade on delete using unidirectional Many-To-Many mapping How to remove entity with ManyToMany relationship in JPA (and corresponding join table rows)? JPA JPQL: select items when attribute of item (list/set) contains another item

@ManyToMany 映射中删除条目时,需要手动调整以删除外键。

多年来我一直在使用自己的 JPA CRUD,并为此提供了一个很好的解决方案:CrudDao 例如NoteDao.

但我最近对Spring JPA 以及通过声明接口然后在@Entity 上创建类似名称的namedQuerys 来自动生成 Cruds 的能力印象深刻。

但是如何解决@ManyToMany 删除问题?我基本上会在我的 Crud 上使用 remove 方法来公开 JPA 的这个“特性”。

【问题讨论】:

【参考方案1】:

我似乎已经使用@PreRemove 解决了这个问题,如https://***.com/a/14911910/1041691 中所述

例如 https://github.com/fommil/zibaldone/blob/master/src/main/java/com/github/fommil/zibaldone/Note.java#L74

【讨论】:

以上是关于Spring JPA 并从 @ManyToMany 中删除条目的主要内容,如果未能解决你的问题,请参考以下文章