2017-02-15 4 views
1

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を閉じる必要はありません。そして、あなたはあなたのトランザクションを管理するための容器を使用しているか、それらを手動で管理する必要があるかどうかによって

答えて

0

あなたのためにそれを実行します。

@PersistenceContext 
EntityManager entityManager; 

public ArrayList<Famille> getListFamille(){ 

    ArrayList<Famille> listFamille = new ArrayList<Famille>(); 
    listFamille.addAll(entityManager.createQuery("from Famille", Famille.class).getResultList()); 

    // entityManager.close(); 

    return listFamille; 
} 

マニュアル管理

あなたが書いたものにaddtionallyトランザクションを作成し、閉じる必要があります:

public ArrayList<Famille> getListFamille(){ 

     entityManager = entityManagerFactory.createEntityManager(); 
     entityManager.getTransaction().begin(); 

     ArrayList<Famille> listFamille = new ArrayList<Famille>(); 
     listFamille.addAll(entityManager.createQuery("from Famille", Famille.class).getResultList()); 

     entityManager.getTransaction().commit(); 
     entityManager.close(); 

     return listFamille; 
    } 

更新

エラーがそれを示していないが、あなたのマッピングは少し奇妙です。

マッピングの両側を熱心に取得しているようで、循環参照の問題が発生する可能性があります。

私は関係すなわちの片側からフェッチEAGERを削除することをお勧め:

@ManyToMany 
@JoinTable(name = "famille_personne", 
     [email protected](name="personne_id"), 
     [email protected](name="famille_id")) 
private Set<Famille> listFamille; 
+0

あなたが言った、それはまだ動作しませんように私は私の2つの方法を変えてきました。なぜ私はトランザクションを開始/コミットする必要がありますか?私はいくつかの値を取得していたときに前にこの方法をやったことはありませんそれは常に働いた –

+0

私はそれが両側に熱心なフェッチの問題かもしれないと思う..私の更新された答えを確認してください –

+0

返信を長らく残念。あなたが言ったように私は試してみたが、私は "遅れて役割のコレクションを初期化できませんでした"という例外を得る。時々私はPersonne.listFamilleを取得したいと思っています。時にはFamille.listPersonneが機能しないようにします。 –

関連する問題