jpa中如果要级联删除一方对应的多方的记录,必须在多对建立与一方之间的多对一关系,否则级联删除不了,如下:
public class ChannelEntity implements java.io.Serializable { .... private List<ChannelDayLimitEntity> chanelDayLimits; @OneToMany(mappedBy="channel", cascade=CascadeType.REMOVE,orphanRemoval=true) public List<ChannelDayLimitEntity> getChanelDayLimits() { return chanelDayLimits; } public void setChanelDayLimits(List<ChannelDayLimitEntity> chanelDayLimits) { this.chanelDayLimits = chanelDayLimits; } } public class ChannelDayLimitEntity implements java.io.Serializable { ... private ChannelEntity channel; @ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE }, optional = true) @JoinColumn(name = "channel_id") public ChannelEntity getChannel() { return channel; } public void setChannel(ChannelEntity channel) { this.channel = channel; } .... }
如果只建立了单身的一对多关系,则只会执行update语句把多那方的关联字段设置为空.
时间: 2024-10-10 06:09:28