I am having trouble with a Many to Many relationship that i have mapped with hibernate! I have 3 tables:
Now, everything is working fine except when i try to delete a resource that has a reference to "clients_access_resources", it gives " Referential integrity constraint violation". But the wierd thing is that, if i delete a Client that has a reference to "clients_access_resource" it works, no problem here!
Client
@ManyToMany(fetch = FetchType.EAGER, targetEntity=Resources.class)
@JoinTable(name = "clients_access_resources", joinColumns = { @JoinColumn(name = "client_id") }, inverseJoinColumns = { @JoinColumn(name = "res_id") })
public Set<Resources> getClientResources() {
return this.clientResources;
}
Resources
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "clientResources")
public Set<ClientsBasic> getClientsBasics() {
return this.clientsBasics;
}
Resources DAO
public void delete(Resources res) throws HibernateException{
synchronized (this) {
Transaction tx = null;
session = this.sessionFactory.openSession();
tx = session.beginTransaction();
session.delete(res);
tx.commit();
session.close();
}
}
Clients DAO
public ClientsBasic save(ClientsBasic client) throws HibernateException {
synchronized (this) {
Transaction tx = null;
session = this.sessionFactory.openSession();
tx = session.beginTransaction();
session.saveOrUpdate(client);
tx.commit();
session.close();
return client;
}
}
I've tried to add "cascade = CascadeType.ALL" but it didn't work.
Can anyone please help me? Thank you.
The mapped by property in the resourses class at the clients getter makes this side the inverse side of the relationship. To make these equal in managing the relationship change this to be the same as in the client class like this
Resources
@ManyToMany(fetch = FetchType.EAGER, targetEntity=ClientsBasic.class)
@JoinTable(name = "clients_access_resources",
joinColumns = {@JoinColumn(name ="res_id")},
inverseJoinColumns = {@JoinColumn(name = "client_id")})
public Set<ClientsBasic> getClientsBasics() {
return this.clientsBasics;
}
Note that the columns' names have been interchanged. I hope this solves your problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.