本文介绍了@ManyToMany级联= CascadeType.REMOVE删除关联和实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有2个实体: / code>和分组。 我想让Hibernate删除关联,但不是删除分组实体的代码: @Autowired private final GroupedRepository groupedRepository; public void delete(grouped groupedToRemove){ groupedRepository.delete(groupedToRemove);如果我将 cascade = CascadeType.ALL code>或 cascade = CascadeType.REMOVE ,我的 Group 实体被删除,当我删除分组实体,不仅是关联: $ b @ ManyToMany(cascade = CascadeType.ALL,//与CascadeType.REMOVE相同的行为 mappedBy =分组, targetEntity = Group.class)私人设置< Group> groups = new HashSet<>(); 如果我删除级联,hibernate会尝试设置group_id = null,并且会引发 ModelConstraintException 。我不想将FK设置为空。 组实体: @Entity @Table(name =groups) @Getter @Setter public class Group { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToMany(targetEntity = Grouped.class) @JoinTable( name =association, joinColumns = @JoinColumn(name =group_id,nullable = false,updatable = false), inverseJoinColumns = @JoinColumn(name =grouped_id,nullable = false,updatable = false)) private Set< Grouped> groupped = new HashSet<>(); 分组实体: @Entity @Table(name =grouped) @Getter @Setter public class分组{b $ b @Id @GeneratedValue(strategy = GenerationType.AUTO)私有整数id; @ManyToMany(mappedBy =groupped,targetEntity = Group.class) private Set< Group> groups = new HashSet<>(); } 解决方案这是预期的行为。删除级联意味着:删除此实体时,还要删除关联的实体。在ManyToXxx上没有任何意义,因为很明显,其他实体仍在引用相关实体。 如果您想要删除一个分组,但将关联的组留在那里,您需要先删除两个实体之间的关联: for(Group group:grouped.getGroups()){ group.getGrouped()。remove(grouped); } grouped.getGroups()。clear(); ,然后删除与任何组无关的分组实体。I have 2 entities: Group and Grouped, with 1 ManyToMany association.In database, the Association table has a NOT NULL FK on both Group and Grouped.I want Hibernate to delete the association but not the group when all grouped are deleted.Code to delete a Grouped entity: @Autowiredprivate final GroupedRepository groupedRepository;public void delete(Grouped groupedToRemove) { groupedRepository.delete(groupedToRemove);}If I set cascade = CascadeType.ALL or cascade = CascadeType.REMOVE, my Group entities are deleted when I delete a Grouped entity, not only the associations:@ManyToMany(cascade = CascadeType.ALL, // same behavior with CascadeType.REMOVE mappedBy = "grouped", targetEntity = Group.class)private Set<Group> groups = new HashSet<>();If I remove the cascade, hibernate tries to set group_id=null and it throws a ModelConstraintException. I don't want to set the FK as nullable.Group entity:@Entity@Table(name = "groups")@Getter@Setterpublic class Group { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToMany(targetEntity = Grouped.class) @JoinTable( name = "association", joinColumns = @JoinColumn(name = "group_id", nullable = false, updatable = false), inverseJoinColumns = @JoinColumn(name = "grouped_id", nullable = false, updatable = false) ) private Set<Grouped> grouped= new HashSet<>();}Grouped entity:@Entity@Table(name = "grouped")@Getter@Setterpublic class Grouped { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToMany(mappedBy = "grouped", targetEntity = Group.class) private Set<Group> groups= new HashSet<>();} 解决方案 That's the expected behavior. REMOVE cascading means: when removing this entity, also remove the associated entities. It makes no sense on a ManyToXxx, since obviously, other entities are still referencing the associated entity.If you want to delete a Grouped, but leave the associated Groups there, you need to remove the association between the two entities first:for (Group group : grouped.getGroups()) { group.getGrouped().remove(grouped);}grouped.getGroups().clear();and then remove the Grouped entity, which is not associated to any Group anymore. 这篇关于@ManyToMany级联= CascadeType.REMOVE删除关联和实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 03:55