0

I'm getting this error

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

when trying to convet a Hashset<> to List<>

this is the function

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é"));
    }

I'm getting the error exactyly at :

liste_produits= new ArrayList<Produit>(liste);

this is the mapping file

<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>

can you resolve this? thank you.

Kukeltje
  • 11,924
  • 4
  • 19
  • 44
Badr At
  • 527
  • 6
  • 16
  • Possible duplicate of [org.hibernate.LazyInitializationException - could not initialize proxy - no Session](http://stackoverflow.com/questions/21574236/org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-sess) – Dragan Bozanovic May 20 '17 at 23:38

1 Answers1

0

Please read my article to solve & understand the issue.

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

galovics
  • 2,884
  • 3
  • 15
  • 25