【发布时间】:2014-07-21 10:05:44
【问题描述】:
我在使用 hibernate 映射的多对多关系时遇到问题! 我有 3 张桌子:
- 客户 - 包含有关客户的信息
- 资源 - 包含有关资源的信息
- ClientAccessResource - 包含有关客户端可以访问哪些资源的信息!
现在,一切正常,除了当我尝试删除引用“clients_access_resources”的资源时,它给出了“违反参照完整性约束”。 但奇怪的是,如果我删除一个引用“clients_access_resource”的客户端,它就可以工作,这里没问题!
客户
@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;
}
资源
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "clientResources")
public Set<ClientsBasic> getClientsBasics() {
return this.clientsBasics;
}
资源 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();
}
}
客户 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;
}
}
我尝试添加“cascade = CascadeType.ALL”,但没有成功。
谁能帮帮我? 谢谢。
【问题讨论】:
标签: java database spring hibernate