[英]Hibernate Many to Many integrity constraint violation
我已經用休眠映射了多對多關系,我遇到了麻煩! 我有3張桌子:
現在,一切正常,除非當我嘗試刪除引用了“ 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”,但是沒有用。
誰能幫幫我嗎? 謝謝。
客戶端getter的resourses類中的by by屬性映射使該面成為關系的反面。 為了使這些在關系管理中平等,將其更改為與這樣的客戶端類中的相同
資源資源
@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;
}
請注意,列的名稱已互換。 我希望這可以解決您的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.