I am working with onetomany relationship in hibernate JPA annotations, here is my table and entity class details...
Service (entity class: ServiceRegistration.java)
=======
serviceid
servicename
channel meta table (entity class: Channels.java)
========
channelid
channelname
service_channel (entity class: ServiceChannels.java)
===============
seq_id
serviceid
channelid
Here, service_channel table has serviceid and channelid as forign keys.. I can able to fetch, modify records.
But I couldn't able to delete the service and it's child records.. If I delete service table records, corresponding service_channel table records should get deleted. here is my entity class details...
Also, I am getting duplicate records.. say if a service (service1) has 2 channels associated, when I fetch the service list, I see 2 service1 entries in the list.
serviceregistration.java
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "multichannel_service_channel", joinColumns = {
@JoinColumn(name="serviceid", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="channelid")
}
)
private Set<Channels> channelsInvolved;
@OneToMany(mappedBy="serviceRegistration")
@Cascade(org.hibernate.annotations.CascadeType.REMOVE)
private List<ServiceChannel> serviceChannels;
servicechannel.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column private int servicechannelid;
@ManyToOne
@JoinColumn(name = "ser开发者_Go百科viceid")
private ServiceRegistration serviceRegistration;
@ManyToOne
@JoinColumn(name = "channelid")
private Channels channels;
channels.java
@Id
@Column
private int channelid;
@Column
private String channelname;
@Column
private String channeldescription;
@Column
private boolean isactive;
Please help to resolve this.
You are asking 2 questions now.
You cannot delete service simply because it is referred. You should first delete the referred entities (or update them to make the service not referred).
That means, you should first delete corresponding ServiceChannels before deleting Service.
The "automatic" removal of ServiceChannel through Service can be achieved by cascade type "Delete Orphan" which is Hibernate-specific feature.
For duplicated list, I believe it depends on the HQL you used for retrieving the list. In case you have joined/join-fetched serviceChannels, it will cause duplicated records. You should either use "select distinct" or add a distinct result transformer to handle that.
Are you trying to define a @ManyToMany, or are you trying to emulate a @ManyToMany with two @ManyToOnes? Seems you are doing both, which won't work.
You have two options. First, @ManyToMany:
create table service_channel (
service_id int not null,
channel_id int not null,
primary key (service_id, channel_id));
ServiceRegistration.java
@ManyToMany
@JoinTable(name = "service_channel",
joinColumns = @JoinColumn(name="service_id"),
inverseJoinColumns = @JoinColumn(name="channel_id"))
private Set<Channel> channels;
Channel.java
@ManyToMany(mappedBy = "channels")
private Set<Service> services;
where ServiceChannel.java doesn't exist. Second, you can promote ServiceChannel to entity status, and model the whole thing with two @OneToManys:
create table service_channel (
id int not null primary key,
service_id int not null,
channel_id int not null);
ServiceChannel.java:
@ManyToOne
@JoinColumn(name = "service_id")
private Service service;
@ManyToOne
@JoinColumn(name = "channel_id")
private Channel channel;
Service.java:
@OneToMany(mappedBy = "service")
private ServiceChannel serviceChannel;
Channel.java:
@OneToMany(mappedBy = "channel")
private ServiceChannel serviceChannel;
精彩评论