我们将springboot2与休眠5.2.17一起使用。 我们使用@ManyToOne和@OneToMany休眠状态很奇怪
伪代码
public class OwningSide {
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "owningSide")
@MapKey(name = "keyColumn")
@SortNatural
private SortedMap<Long, NonOwningSide> nonOwningSideMap= new TreeMap<>(naturalOrder());
}
public class NonOwningSide {
@ManyToOne(cascade = CascadeType.ALL, targetEntity = OwningSide.class)
@JoinColumn(name = "owningSide_id")
private OwningSide owningSide;
}
public class Service{
@Transactional
public void removeByKey(OwnerSide ownerSide)
{
SortedMap<Long, NonOwningSide> nonOwningSideMap=ownerSide.getNonOwningSideMap();
nonOwningSideMap.remove(3);
}
}
返回后,休眠,删除2行。
Hibernate: delete from schema.NonOwnerTable where id=? and version=?
Hibernate: delete from schema.NonOwnerTable where id=? and version=?
它将删除我要删除的行以及地图中的第一行。总是第一行。
如果我从@ManyToOne中删除Cascade.ALL,则问题已解决。 但是,我不明白,为什么休眠用Cascade.ALL删除第一行