我有以下实体,当删除ServiceRegistration记录时,我必须删除ServiceRegistration和ServiceChannels条目。但现在,如果我删除了serviceregistration中的记录,它将删除通道中的记录,即元数据表。
ServiceRegistration.Java
@OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name = "ServiceChannel", joinColumns = {
@JoinColumn(name="serviceid", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="channelid")
}
)
private List<Channels> channelsInvolved;
public List<Channels> getChannelsInvolved() {
return channelsInvolved;
}
public void setChannelsInvolved(List<Channels> channelsInvolved) {
this.channelsInvolved = channelsInvolved;
}
ServiceChannels.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column private int servicechannelid;
@ManyToOne
@JoinColumn(name = "serviceid")
private ServiceRegistration serviceRegistration;
@ManyToOne
@JoinColumn(name = "channelid")
private Channels channels;
Channels.java&gt;&gt;包含元数据
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column private int channelid;
@Column private String channelname;
@Override
public boolean equals(final Object obj) {
if (obj instanceof Channels) {
final Channels other = (Channels) obj;
if (other.getChannelid() == getChannelid()) {
return true;
}
}
return false;
}
请帮我看看如何在这个实体关系中进行级联删除。
答案 0 :(得分:0)
首先,如果您不希望在删除ServiceRegistration时删除频道,则应执行以下操作:
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ServiceChannel", joinColumns = {
@JoinColumn(name="serviceid", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="channelid")
}
)
private List<Channels> channelsInvolved;
在您的服务注册中(禁用级联)。
此外,如果要在删除ServiceRegistration时删除ServiceChannel,则必须在ServiceRegistration端配置关系:
@OneToMany(mappedBy="serviceRegistration", cascade=CascadeType.REMOVE) // Edited to specify the owning side of the relationship
private List<ServiceChannel> serviceChannels;