I have the following entities and I have to delete the ServiceRegistration and ServiceChannels entries when I delete the ServiceRegistration record. But now, if I delete a record in serviceregistration, it delete the records in channel which is metadata table.
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 >> contains meta-data
@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;
}
Please help me how to do cascade delete in this entity relation.
First, if you don't want to get channels deleted when deleting a ServiceRegistration, you should do:
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ServiceChannel", joinColumns = {
@JoinColumn(name="serviceid", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="channelid")
}
)
private List<Channels> channelsInvolved;
In your service registration (disable cascading).
Also, if you want to get your ServiceChannel removed when you delete a ServiceRegistration, you have to configure the relationship in the ServiceRegistration side:
@OneToMany(mappedBy="serviceRegistration", cascade=CascadeType.REMOVE) // Edited to specify the owning side of the relationship
private List<ServiceChannel> serviceChannels;
精彩评论