org.hibernate.LazyInitializationException: illegal access to loading collection
Bonjour,
En mappant une relation père fils, je suis confronté à cette exception :
Citation:
Caused by: 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.AbstractPersistentCollection.readElementExistence(AbstractPersistentCollection.java:142)
at org.hibernate.collection.PersistentSet.contains(PersistentSet.java:153)
at be.gervaisb.sics.commons.beans.Student.setClasse(Student.java:53)
... 38 more
Mon code de test est le suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| DAOFactory daof = (DAOFactory) ctx.getBean("daoFactory");
Section section = new Section("Une section");
daof.getSectionDao().save(section);
Option option = new Option("Option", section);
daof.getOptionDao().save(option);
SchoolClass cls1 = new SchoolClass(5, option);
Integer id = daof.getClasseDao().save(cls1);
Student student0 = new Student("Bidon", "Jean");
Student student1 = new Student("Demo", "Damien");
cls1.addStudent(student0);
//cls1.addStudent(student1);
cls1.setSuffix("A");
daof.getClasseDao().update(cls1);
System.out.println("\n INSERT DATAS");
System.out.println("\n["+cls1.getId()+"] "+cls1+"");
for (Student student : cls1.getStudents()) {
System.out.println("\t["+student.getId()+"] "+student+" ("+student.getClasse()+")");
}
id = cls1.getId();
cls1.setStudents(null);
cls1 = null;
System.out.println("\n SELECT DATAS");
SchoolClass cls = daof.getClasseDao().get(id); |
Et l'exception se produit sur la dernière ligne.
Tous mes daos étendent celui-ci (sans surcharger les méthodes) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public class GenericHibernateDao<T , PK extends Serializable> extends HibernateDaoSupport {
protected final static Logger logger = Logger.getLogger();
private Class<T> type;
public GenericHibernateDao(Class<T> type) {
this.type = type;
}
/** Store an object to the storage and return the generated key */
@SuppressWarnings("unchecked")
public PK save(T object) throws DataAccessException {
PK key = (PK) getHibernateTemplate().save(object);
getHibernateTemplate().flush();
//getHibernateTemplate().merge(object);
logger.debug("Saving %s '%s' with key '%s'.", type.getSimpleName(), object, key);
return key;
}
/**
* Retrieve an object that was previously saved to the storage using the
* indicated key as primary key.
*/
@SuppressWarnings("unchecked")
public T get(PK key) throws DataAccessException {
T object = (T) getHibernateTemplate().get(type, key);
logger.debug("Getting %s with key '%s' return '%s'.", type.getSimpleName(), key, object);
return object;
}
/** Save changes made to an object */
public void update(T object) throws DataAccessException {
logger.debug("Updating %s '%s'.", type.getSimpleName(), object);
getHibernateTemplate().update(object);
getHibernateTemplate().flush();
}
/** Remove an object from the storage */
public void remove(T object) throws DataAccessException {
logger.debug("Removing "+object);
getHibernateTemplate().delete(object);
}
} |
Voilà, j'espère que quelqu'un parmi vous pourra sm'aider à m'en sortir car je suis complètement perdu..
J'ai vu que cela pouvais-être du à la méthode hashCode() mais j'ai essayé différents façons de la surcharger dans 'Student' et 'SchoolClass' mais j'obtiens toujours la même erreur.
Mes deux objets 'Student' et 'SchoolClass' n'ont rien d'exceptionnel mais si vous le voulez je peux également les déposer ici..
MERCI
Edit : J'ai suivi le tutoriels suivant http://www.hibernate.org/hib_docs/v3...rentchild.html jusqu'au chapitre 21.3 mais sans placer de "not-null" ni de "all-delete-orphan" car pour moi un élève peut exister dans être dans une classe.