These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: many -to-many marche pas : LazyInitializationException
PostPosted: Wed Jan 10, 2007 9:46 am 
Newbie

Joined: Wed Jan 10, 2007 9:37 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3


L'enregistrement dans la table de liaison ne ce fait jamais ! j'ai ajouter les hashcode mais ca ma fait l'erreur sur les hashcode propriete.hashcode() qui est un Set.


3 tables :
films
propriete
film_has propriete

2 objets:
films
propriete



Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 9 janv. 2007 22:59:11 by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="com.db.hibernate.Films" table="films" catalog="vidix">
<comment></comment>
<id name="idfilms" type="int">
<column name="idfilms" />
<generator class="assigned" />
</id>
<property name="titre" type="string">
<column name="titre" length="45">
<comment></comment>
</column>
</property>
<property name="date" type="date">
<column name="date" length="10">
<comment></comment>
</column>
</property>
<property name="duree" type="time">
<column name="duree" length="8">
<comment></comment>
</column>
</property>
<property name="numExploit" type="string">
<column name="numExploit" length="45">
<comment></comment>
</column>
</property>
<property name="synopsis" type="string">
<column name="synopsis" length="65535">
<comment></comment>
</column>
</property>
<set name="sequenceses" inverse="true">
<key>
<column name="films_idfilms">
<comment></comment>
</column>
</key>
<one-to-many class="com.db.hibernate.Sequences" />
</set>
<set name="proprieteses" inverse="true" table="films_has_proprietes">
<key>
<column name="films_idfilms" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="com.db.hibernate.Proprietes">
<column name="proprietes_idproprietes" not-null="true">
<comment></comment>
</column>
</many-to-many>
</set>
<set name="fichesServiceses" inverse="true">
<key>
<column name="films_idfilms" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="com.db.hibernate.FichesServices" />
</set>
<set name="documentses" inverse="true" table="films_has_documents">
<key>
<column name="films_idfilms" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="com.db.hibernate.Documents">
<column name="documents_iddocuments" not-null="true">
<comment></comment>
</column>
</many-to-many>
</set>
</class>
</hibernate-mapping>



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 9 janv. 2007 22:59:11 by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="com.db.hibernate.Proprietes" table="proprietes" catalog="vidix">
<comment></comment>
<id name="idproprietes" type="int">
<column name="idproprietes" />
<generator class="assigned" />
</id>
<many-to-one name="nomProprietes" class="com.db.hibernate.NomProprietes" fetch="select">
<column name="nomProprietes_idnomProprietes" not-null="true">
<comment></comment>
</column>
</many-to-one>
<property name="valeur" type="string">
<column name="valeur" length="45">
<comment></comment>
</column>
</property>
<set name="filmses" inverse="true" table="films_has_proprietes">
<key>
<column name="proprietes_idproprietes" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="com.db.hibernate.Films">
<column name="films_idfilms" not-null="true">
<comment></comment>
</column>
</many-to-many>
</set>
</class>
</hibernate-mapping>


mes 2 classes pojo
Code:

package com.db.hibernate;

// Generated 9 janv. 2007 22:59:11 by Hibernate Tools 3.2.0.beta8

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
* Films generated by hbm2java
*/
public class Films implements java.io.Serializable
{

   // Fields   

   private int idfilms;

   private String titre;

   private Date date;

   private Date duree;

   private String numExploit;

   private String synopsis;

   private Set<Sequences> sequenceses = new HashSet<Sequences>(0);

   private Set<Proprietes> proprieteses = new HashSet<Proprietes>(0);

   private Set<FichesServices> fichesServiceses = new HashSet<FichesServices>(0);

   private Set<Documents> documentses = new HashSet<Documents>(0);

   // Constructors

   /** default constructor */
   public Films()
   {
   }

   /** minimal constructor */
   public Films(int idfilms)
   {
      this.idfilms = idfilms;
   }

   /** full constructor */
   public Films(int idfilms, String titre, Date date, Date duree, String numExploit, String synopsis, Set<Sequences> sequenceses, Set<Proprietes> proprieteses, Set<FichesServices> fichesServiceses, Set<Documents> documentses)
   {
      this.idfilms = idfilms;
      this.titre = titre;
      this.date = date;
      this.duree = duree;
      this.numExploit = numExploit;
      this.synopsis = synopsis;
      this.sequenceses = sequenceses;
      this.proprieteses = proprieteses;
      this.fichesServiceses = fichesServiceses;
      this.documentses = documentses;
   }

   // Property accessors
   public int getIdfilms()
   {
      return this.idfilms;
   }

   public void setIdfilms(int idfilms)
   {
      this.idfilms = idfilms;
   }

   public String getTitre()
   {
      return this.titre;
   }

   public void setTitre(String titre)
   {
      this.titre = titre;
   }

   public Date getDate()
   {
      return this.date;
   }

   public void setDate(Date date)
   {
      this.date = date;
   }

   public Date getDuree()
   {
      return this.duree;
   }

   public void setDuree(Date duree)
   {
      this.duree = duree;
   }

   public String getNumExploit()
   {
      return this.numExploit;
   }

   public void setNumExploit(String numExploit)
   {
      this.numExploit = numExploit;
   }

   public String getSynopsis()
   {
      return this.synopsis;
   }

   public void setSynopsis(String synopsis)
   {
      this.synopsis = synopsis;
   }

   public Set<Sequences> getSequenceses()
   {
      return this.sequenceses;
   }

   public void setSequenceses(Set<Sequences> sequenceses)
   {
      this.sequenceses = sequenceses;
   }

   public Set<Proprietes> getProprieteses()
   {
      return this.proprieteses;
   }

   public void setProprieteses(Set<Proprietes> proprieteses)
   {
      this.proprieteses = proprieteses;
   }

   public Set<FichesServices> getFichesServiceses()
   {
      return this.fichesServiceses;
   }

   public void setFichesServiceses(Set<FichesServices> fichesServiceses)
   {
      this.fichesServiceses = fichesServiceses;
   }

   public Set<Documents> getDocumentses()
   {
      return this.documentses;
   }

   public void setDocumentses(Set<Documents> documentses)
   {
      this.documentses = documentses;
   }

   /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
   @Override
   public int hashCode()
   {
      final int PRIME = 31;
      int result = 1;
      result = PRIME * result + ((date == null) ? 0 : date.hashCode());
      result = PRIME * result + ((documentses == null) ? 0 : documentses.hashCode());
      result = PRIME * result + ((duree == null) ? 0 : duree.hashCode());
      result = PRIME * result + ((fichesServiceses == null) ? 0 : fichesServiceses.hashCode());
      result = PRIME * result + idfilms;
      result = PRIME * result + ((numExploit == null) ? 0 : numExploit.hashCode());
      
      Set test=proprieteses;
      
      int testhc=test.hashCode();
      
      result = PRIME * result + (
            (proprieteses == null)
            ? 0
                  : proprieteses.hashCode());
      result = PRIME * result + ((sequenceses == null) ? 0 : sequenceses.hashCode());
      result = PRIME * result + ((synopsis == null) ? 0 : synopsis.hashCode());
      result = PRIME * result + ((titre == null) ? 0 : titre.hashCode());
      return result;
   }

   /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
   @Override
   public boolean equals(Object obj)
   {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      final Films other = (Films) obj;
      if (date == null)
      {
         if (other.date != null)
            return false;
      } else if (!date.equals(other.date))
         return false;
      if (documentses == null)
      {
         if (other.documentses != null)
            return false;
      } else if (!documentses.equals(other.documentses))
         return false;
      if (duree == null)
      {
         if (other.duree != null)
            return false;
      } else if (!duree.equals(other.duree))
         return false;
      if (fichesServiceses == null)
      {
         if (other.fichesServiceses != null)
            return false;
      } else if (!fichesServiceses.equals(other.fichesServiceses))
         return false;
      if (idfilms != other.idfilms)
         return false;
      if (numExploit == null)
      {
         if (other.numExploit != null)
            return false;
      } else if (!numExploit.equals(other.numExploit))
         return false;
      if (proprieteses == null)
      {
         if (other.proprieteses != null)
            return false;
      } else if (!proprieteses.equals(other.proprieteses))
         return false;
      if (sequenceses == null)
      {
         if (other.sequenceses != null)
            return false;
      } else if (!sequenceses.equals(other.sequenceses))
         return false;
      if (synopsis == null)
      {
         if (other.synopsis != null)
            return false;
      } else if (!synopsis.equals(other.synopsis))
         return false;
      if (titre == null)
      {
         if (other.titre != null)
            return false;
      } else if (!titre.equals(other.titre))
         return false;
      return true;
   }

}



package com.db.hibernate;

// Generated 9 janv. 2007 22:59:11 by Hibernate Tools 3.2.0.beta8

import java.util.HashSet;
import java.util.Set;

/**
* Proprietes generated by hbm2java
*/
public class Proprietes implements java.io.Serializable
{

   // Fields   

   private int idproprietes;

   private NomProprietes nomProprietes;

   private String valeur;

   private Set<Films> filmses = new HashSet<Films>(0);

   // Constructors

   /** default constructor */
   public Proprietes()
   {
   }

   /** minimal constructor */
   public Proprietes(int idproprietes, NomProprietes nomProprietes)
   {
      this.idproprietes = idproprietes;
      this.nomProprietes = nomProprietes;
   }

   /** full constructor */
   public Proprietes(int idproprietes, NomProprietes nomProprietes, String valeur, Set<Films> filmses)
   {
      this.idproprietes = idproprietes;
      this.nomProprietes = nomProprietes;
      this.valeur = valeur;
      this.filmses = filmses;
   }

   // Property accessors
   public int getIdproprietes()
   {
      return this.idproprietes;
   }

   public void setIdproprietes(int idproprietes)
   {
      this.idproprietes = idproprietes;
   }

   public NomProprietes getNomProprietes()
   {
      return this.nomProprietes;
   }

   public void setNomProprietes(NomProprietes nomProprietes)
   {
      this.nomProprietes = nomProprietes;
   }

   public String getValeur()
   {
      return this.valeur;
   }

   public void setValeur(String valeur)
   {
      this.valeur = valeur;
   }

   public Set<Films> getFilmses()
   {
      return this.filmses;
   }

   public void setFilmses(Set<Films> filmses)
   {
      this.filmses = filmses;
   }

}



Code between sessionFactory.openSession() and session.close():

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Films f = (Films) session.load(Films.class, 1);

Set p=f.getProprieteses();
System.out.println(">>>>>>>>>>>>>< film titre : " + f.getTitre());
Iterator ps= p.iterator();

for (; ps.hasNext();)
{
Proprietes element = (Proprietes) ps.next();


System.out.println("!!!!!!!!!!!!!!!!!!!!!!! \n film titre : " + element.getValeur() + " Time: " + element.getNomProprietes().getNom());
}



NomProprietes np=(NomProprietes) session.load(NomProprietes.class , 1);
Proprietes pp=new Proprietes();
pp.setNomProprietes(np);
pp.setValeur("finlandais");


ps= f.getProprieteses().iterator();

for (; ps.hasNext();)
{
Proprietes element = (Proprietes) ps.next();


System.out.println("film titre : " + element.getValeur() + " Time: " + element.getNomProprietes().getNom());
}
//session.saveOrUpdate(f);





f.getProprieteses().add(pp);


pp.getFilmses().add(f);

session.save(pp);
//session.update(f);


session.getTransaction().commit();

HibernateUtil.getSessionFactory().close();





Full stack trace of any exception that occurs:

23:05:17,446 DEBUG SQL:393 - select fichesserv0_.films_idfilms as films2_1_, fichesserv0_.idfichesServices as idfiches1_1_, fichesserv0_.idfichesServices as idfiches1_16_0_, fichesserv0_.films_idfilms as films2_16_0_, fichesserv0_.date as date16_0_ from vidix.fichesServices fichesserv0_ where fichesserv0_.films_idfilms=? Hibernate: select fichesserv0_.films_idfilms as films2_1_, fichesserv0_.idfichesServices as idfiches1_1_, fichesserv0_.idfichesServices as idfiches1_16_0_, fichesserv0_.films_idfilms as films2_16_0_, fichesserv0_.date as date16_0_ from vidix.fichesServices fichesserv0_ where fichesserv0_.films_idfilms=? 23:05:17,447 DEBUG AbstractBatcher:476 - preparing statement 23:05:17,452 DEBUG IntegerType:80 - binding '1' to parameter: 1 23:05:17,457 DEBUG AbstractBatcher:374 - about to open ResultSet (open ResultSets: 0, globally: 0) 23:05:17,459 DEBUG Loader:1040 - result set contains (possibly empty) collection: [com.db.hibernate.Films.fichesServiceses#1] 23:05:17,459 DEBUG CollectionLoadContext:84 - uninitialized collection: initializing 23:05:17,460 DEBUG Loader:682 - processing result set 23:05:17,462 DEBUG Loader:709 - done processing result set (0 rows) 23:05:17,463 DEBUG AbstractBatcher:381 - about to close ResultSet (open ResultSets: 1, globally: 1) 23:05:17,464 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 23:05:17,465 DEBUG AbstractBatcher:525 - closing statement 23:05:17,466 DEBUG Loader:839 - total objects hydrated: 0 23:05:17,469 DEBUG CollectionLoadContext:262 - 1 collections were found in result set for role: com.db.hibernate.Films.fichesServiceses 23:05:17,469 DEBUG CollectionLoadContext:206 - collection fully initialized: [com.db.hibernate.Films.fichesServiceses#1] 23:05:17,470 DEBUG CollectionLoadContext:272 - 1 collections initialized for role: com.db.hibernate.Films.fichesServiceses 23:05:17,471 DEBUG Loader:1934 - done loading collection 23:05:17,473 DEBUG DefaultInitializeCollectionEventListener:61 - collection initialized 23:05:23,679 ERROR LazyInitializationException:19 - illegal access to loading collection org.hibernate.LazyInitializationException: illegal access to loading collection at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86) at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:256) at com.db.hibernate.Films.hashCode(Films.java:181) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:185) at com.db.hibernate.Films$$EnhancerByCGLIB$$84eb8843.hashCode(<generated>) at java.util.HashMap.hash(HashMap.java:264) at java.util.HashMap.put(HashMap.java:382) at java.util.HashSet.add(HashSet.java:194) at java.util.AbstractCollection.addAll(AbstractCollection.java:318) at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:276) at org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:183) at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:268) at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:249) at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:866) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:853) at org.hibernate.loader.Loader.doQuery(Loader.java:717) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.loadCollection(Loader.java:1918) at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86) at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:358) at com.db.hibernate.Proprietes.hashCode(Proprietes.java:96) at java.util.HashMap.hash(HashMap.java:264) at java.util.HashMap.put(HashMap.java:382) at java.util.HashSet.add(HashSet.java:194) at java.util.AbstractCollection.addAll(AbstractCollection.java:318) at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:276) at org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:183) at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:268) at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:249) at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:866) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:853) at org.hibernate.loader.Loader.doQuery(Loader.java:717) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.loadCollection(Loader.java:1918) at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86) at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:141) at test.Test1.main(Test1.java:32)



Name and version of the database you are using:
mysql 5



merci...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 10, 2007 12:57 pm 
Newbie

Joined: Wed Jan 10, 2007 9:37 am
Posts: 2
Code:
Set test=proprieteses;
int testhc=test.hashCode();   <------ apparement l'erreur viendrais d'ici !
result = PRIME * result + ((proprieteses == null)....



Est-ce que redefinir les methode hashcode et equal est vraiment nécessaire ?

avec j'ai l'exception LazyInitializationException

et sans je n'ai pas d'enregistrement


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.