org.hibernate.LazyInitializationException:无法懒惰地初始化角色集合:

时间:2017-05-20 13:16:59

标签: hibernate

我收到此错误

org.hibernate.LazyInitializationException: 
failed to lazily initialize a collection of role: com.InventoryProject.Entity.Decharge.produits, 
could not initialize proxy - no Session

当试图传达Hashset时<>列出<>

这是功能

public void SaveToPrint() throws Exception{


        int id_decharge = ServiceFactory.getDechargeservice().derniereDecharge();
        Decharge decharge = ServiceFactory.getDechargeservice().FindDecharge(id_decharge);
        decharge.setObservations(obsrv);
        ServiceFactory.getDechargeservice().ModifierDecharge(decharge);

        Set<Produit> liste =  new HashSet<Produit>();
        liste = ServiceFactory.getDechargeservice().getDechargeById(id_decharge).getProduits();

        liste_produits= new ArrayList<Produit>(liste);

        ServiceFactory.getProduitservice().supprimerConsomable();

        FacesContext.getCurrentInstance().addMessage(null, 
                new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "Opération terminé"));
    }

我正确地收到错误:

liste_produits= new ArrayList<Produit>(liste);

这是映射文件

<hibernate-mapping>
   <class name="com.InventoryProject.Entity.Decharge" table="DECHARGE">
      <meta attribute="class-description">
         This class contains the product detail. 
      </meta>
      <id name="id" type="int" column="ID_DECHARGE">
         <generator class="native"/>
      </id>
      <property name="beneficiaire" column="BENEFICIAIRE" type="string"/>
      <property name="observations" column="OBSERVATIONS" type="string"/>
      <property name="type" column="TYPE" type="string"/>
      <property name="prix" column="PRIX" type="int"/>
      <property name="date" column="DATE" type="string"/>

      <set name="produits" table="PR_DECHARGE">
        <key column="ID_DECHARGE"></key>
        <many-to-many class="com.InventoryProject.Entity.Produit" column="ID_PRODUIT"></many-to-many>
      </set>
   </class>
</hibernate-mapping>
你可以解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

请阅读我的文章以解决&amp;了解这个问题。

http://blog.arnoldgalovics.com/2017/02/27/lazyinitializationexception-demystified/