したがって、@ Al1のmapped by
アノテーションのおかげで、問題の解決策が見つかりました。それでも、LazyInitializationException
のためにオブジェクトを取得できませんでしたが、ツリー内のLeafを削除できました。 、私はHibernate.initialize(this.getChildren());
によって子をロードするためにしなければならなかったツリーノードを削除するために
public class Person implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@ManyToOne(cascade = {CascadeType.ALL})
private Person father;
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, mappedBy= "father")
private Collection<Person> children = new LinkedHashSet<Person>();
}
して、再帰的にすべてのを削除します。 は、私はクラスが今のように見えるprivate List<Person> children= new ArrayList<Person>();
private Collection<Person> children = new LinkedHashSet<Person>();
に変更することで、その問題を修正しましたノード。削除のMy機能:
public static String deletePerson(Person p){
Transaction trns = null;
Session session = HibernateUtil.buildSessionFactory().openSession();
try {
trns = session.beginTransaction();
Hibernate.initialize(p.getChildren());
if (p.hasChildren()){
Collection<Person> children = p.getChildren();
for (Person person : children) {
deletePerson(person);
}
String hql = "delete from Person where name = :name";
session.createQuery(hql).setString("name", p.getName()).executeUpdate();
session.getTransaction().commit();
return "success";
}
else {
String hql = "delete from Person where name = :name";
session.createQuery(hql).setString("name", p.getName()).executeUpdate();
session.getTransaction().commit();
return "success";
}
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return "failure";
}
希望これはHibernateと木で動作する誰かを助け:)
外部キーはあなたが – JustMe
がどのように追加することができますやりたいDELETE/UPDATE CASCADE'の整合性制約に '持っている必要がありますそれ? – FalseScience
@ JustMeが述べたように、あなたのエラーはおそらくデータベースレベルです。 [データベースの外部キー定義](https://www.postgresql.org/docs/9.6/static/ddl-constraints.html#DDL-CONSTRAINTS-FK)を見てください。さらに、 '' mappedBy = "..." 'annotation](https://stackoverflow.com/q/11938253/4906586)をあなたの' children'定義に使用したくないのですか? – Al1