2つのエンティティの間にManyToMany関係があります。何らかの理由で、データベースからすべてのエンティティを取得しようとすると、私は"statement closed"
例外が発生します。ここでManyToMany関連要素を取得するときのStatementクローズ例外
はいくつかのコードです:
public class Famille {
[...]
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "famille_personne",
[email protected](name="famille_id"), [email protected](name="personne_id"))
private Set<Personne> listPersonne;
[...]
@Override
public boolean equals(Object object){
if(object instanceof Famille){
Famille famille = (Famille) object;
return (nom.equals(famille.getNom())
&& ((listPersonne == null && famille.getListPersonne() == null)
|| (listPersonne != null && new HashSet(listPersonne).equals(new HashSet(famille.getListPersonne())))
)
);
}
return false;
}
}
public class Personne {
[...]
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "famille_personne",
[email protected](name="personne_id"), [email protected](name="famille_id"))
private Set<Famille> listFamille;
[...]
@Override
public boolean equals(Object object){
if(object instanceof Personne){
Personne personne = (Personne) object;
return (matricule.equals(personne.getMatricule()) && nom.equals(personne.getNom()));
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(matricule, nom);
}
}
そして、ここでは、私は同じ時間で実行することができない2つのクエリを持つ方法である:ここでは
public ArrayList<Famille> getListFamille(){
entityManager = entityManagerFactory.createEntityManager();
ArrayList<Famille> listFamille = new ArrayList<Famille>();
listFamille.addAll(entityManager.createQuery("from Famille", Famille.class).getResultList());
entityManager.close();
return listFamille;
}
public ArrayList<Personne> getListPersonne(){
entityManager = entityManagerFactory.createEntityManager();
ArrayList<Personne> listPersonne = new ArrayList<Personne>();
listPersonne.addAll(entityManager.createQuery("from Personne", Personne.class).getResultList());
entityManager.close();
return listPersonne;
}
は、私が得た例外です:
org.postgresql.util.PSQLException: This statement has been closed.
どのように動作するのか、なぜこの例外が発生するのか説明できますか?
Container Managerの
をあなただけのコンテナを聞かせて、手動でEntityManagerを閉じる必要はありません。そして、あなたはあなたのトランザクションを管理するための容器を使用しているか、それらを手動で管理する必要があるかどうかによって
あなたが言った、それはまだ動作しませんように私は私の2つの方法を変えてきました。なぜ私はトランザクションを開始/コミットする必要がありますか?私はいくつかの値を取得していたときに前にこの方法をやったことはありませんそれは常に働いた –
私はそれが両側に熱心なフェッチの問題かもしれないと思う..私の更新された答えを確認してください –
返信を長らく残念。あなたが言ったように私は試してみたが、私は "遅れて役割のコレクションを初期化できませんでした"という例外を得る。時々私はPersonne.listFamilleを取得したいと思っています。時にはFamille.listPersonneが機能しないようにします。 –