Hibernate version:
3.0.2
Hi,
I have a bidirectionnal many to many relationship that causes a LazyInitializationException (see below).
A job could have one or many contacts for the client and a contact could be associated with one or many jobs.
I dont understand where the problem is, so I have tried to specify "lazy=false" at both ends of my relationship, but the problem remains... How could a LazyInitializationException occurs with "lazy=false" ?
Please see below for the entire exception and the concerned code.
I have spent long time to search for a solution, so thank you in advance for any help
Best regards
Lilian
Mapping documents:
Job.hbm.xml :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="bab.admin.model.persistent.Job" table="t_service"
dynamic-update="false" dynamic-insert="false"
select-before-update="false" optimistic-lock="version">
<id name="id" column="serviceId" type="java.lang.Integer">
<generator class="native">
</generator>
</id>
<many-to-one
name="client"
column="clientPartyId"
lazy="true" /><!-- modifié à true par LR 27 avril 05 -->
<many-to-one
name="contactForBAB"
column="employeePartyId"
lazy="true" /><!-- modifié à true par LR 27 avril 05 -->
<set
name="contactsForClient"
table="t_clientcontact"
inverse="true"
cascade="all"
lazy="false">
<key column="serviceId" />
<many-to-many
class="bab.admin.model.persistent.Contact"
column="partyId" />
</set>
<set
name="periods"
inverse="true"
cascade="all"
order-by="startDate">
<key column="serviceId" />
<one-to-many
class="bab.admin.model.persistent.JobPeriod" />
</set>
<set
name="jobWorkers"
inverse="true"
cascade="all" >
<key column="serviceId" />
<one-to-many
class="bab.admin.model.persistent.JobWorker" />
</set>
<!--<many-to-one
name="contactForClient"
column="clientResponsiblePartyId"
class="bab.admin.model.persistent.Contact"
cascade="save-update"
lazy="false" /> -->
<many-to-one
name="tarif"
column="tarifId"
cascade="all"
lazy="true" /><!-- modifié à true par LR 27 avril 05 -->
<one-to-one name="evaluation" />
<many-to-one
name="address"
cascade="all"
column="contactPointId" />
<property name="fileId" />
<property
name="title"
column="jobTitle" />
<property
name="description"
column="jobDescription" />
<property
name="begin"
column="contactDate" />
<property
name="close"
column="closeDate" />
<property
name="contract"
column="contractDate" />
<property
name="feedback"
column="babFeedback" />
<property
name="comment" />
<component name="invoice">
<property
name="sent"
column="invoiceDate" />
<property
name="number"
column="invoiceNumber" />
<property name="comment"
column="invoiceComment" />
<property name="otherCharges" />
<property name="otherChargesDescription" />
</component>
</class>
</hibernate-mapping>
Contact.hbm.xml :Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<subclass
name="bab.admin.model.persistent.Contact"
discriminator-value="contact"
extends="bab.admin.model.persistent.IParty" >
<set
name="jobs"
table="t_clientcontact"
cascade="all" lazy="false">
<key column="partyId" />
<many-to-many
class="bab.admin.model.persistent.Job"
column="serviceId" >
</many-to-many>
</set>
<join table="t_person">
<key column="partyId" />
<property name="sex" />
<property name="title" />
<property name="maritalStatus" column="maritalStatusCode" />
<property name="birthdate" />
<component
name="fullname"
class="bab.admin.model.persistent.Fullname" >
<property name="firstname" />
<property name="lastname" />
</component>
</join>
</subclass>
</hibernate-mapping>
addJob method in Contact class:Code:
public void addJob( Job job )
{
jobs.add( job );
}
setContactsForClient method in Job class:Code:
public void setContactsForClient( Collection contactsForClient )
{
//déassocier les anciens contacts :
Collection currentContacts = getContactsForClient();
for ( Iterator iter = currentContacts.iterator(); iter.hasNext(); )
{
Contact contact = (Contact)iter.next();
contact.removeJob( this );
}
//associer les nouveaux contacts
for ( Iterator iter = contactsForClient.iterator(); iter.hasNext(); )
{
Contact contact = (Contact)iter.next();
contact.addJob( this );
}
this.contactsForClient = contactsForClient;
}
findAll method in DefaultDAO class :Code:
public Collection findAll( Class clazz ) throws InfrastructureException
{
try
{
Session session = HibernateUtil.getSession();
Criteria crit = session.createCriteria( clazz );
return crit.list();
}
catch ( HibernateException ex )
{
throw new InfrastructureException( ex );
}
}
testFindAll method in DefaultDAOTest class :Code:
public void testFindAll()
{
Collection objects = dao.findAll( Contact.class );
assertNotNull( objects );
for ( Iterator iter = objects.iterator(); iter.hasNext(); )
{
Contact o = (Contact)iter.next();
System.out.println( o.getFullname() );
System.out.println( "==========================" );
Collection jobs = o.getJobs();
for ( Iterator iterator = jobs.iterator(); iterator.hasNext(); )
{
Job job = (Job)iterator.next();
System.out.println( " " +job.getTitle() );
}
}
}
Full stack trace of any exception that occurs:
09:56:55,563 ERROR LazyInitializationException:19 - cannot access loading collection
org.hibernate.LazyInitializationException: cannot access loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:168)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:61)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:158)
at bab.admin.model.persistent.Contact.addJob(Contact.java:84)
at bab.admin.model.persistent.Job.setContactsForClient(Job.java:426)
at bab.admin.model.persistent.Job$$BulkBeanByCGLIB$$9a708169.setPropertyValues(<generated>)
at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:200)
at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:173)
at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2900)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:506)
at org.hibernate.loader.Loader.doQuery(Loader.java:415)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:210)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1396)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:107)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:483)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1354)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:271)
at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(PersistenceContext.java:796)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:215)
at org.hibernate.loader.Loader.doList(Loader.java:1557)
at org.hibernate.loader.Loader.list(Loader.java:1540)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:113)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1254)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)
at bab.admin.model.dao.DefaultDAO.findAll(DefaultDAO.java:90)
at bab.admin.model.dao.DefaultDAOTest.testFindAll(DefaultDAOTest.java:68)
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 junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
Name and version of the database you are using:
MySQL 4.1
The generated SQL (show_sql=true):
Hibernate: select this_.partyId as partyId0_, this_.comment as comment4_0_, this_1_.sex as sex6_0_, this_1_.title as title6_0_, this_1_.maritalStatusCode as maritalS4_6_0_, this_1_.birthdate as birthdate6_0_, this_1_.firstname as firstname6_0_, this_1_.lastname as lastname6_0_ from t_party this_ inner join t_person this_1_ on this_.partyId=this_1_.partyId where this_.partyType='contact'
Hibernate: select jobs0_.partyId as partyId__, jobs0_.serviceId as serviceId__, job1_.serviceId as serviceId0_, job1_.clientPartyId as clientPa2_11_0_, job1_.employeePartyId as employee3_11_0_, job1_.tarifId as tarifId11_0_, job1_.contactPointId as contactP5_11_0_, job1_.fileId as fileId11_0_, job1_.jobTitle as jobTitle11_0_, job1_.jobDescription as jobDescr8_11_0_, job1_.contactDate as contactD9_11_0_, job1_.closeDate as closeDate11_0_, job1_.contractDate as contrac11_11_0_, job1_.babFeedback as babFeed12_11_0_, job1_.comment as comment11_0_, job1_.invoiceDate as invoice14_11_0_, job1_.invoiceNumber as invoice15_11_0_, job1_.invoiceComment as invoice16_11_0_, job1_.otherCharges as otherCh17_11_0_, job1_.otherChargesDescription as otherCh18_11_0_, clienteval2_.evaluationId as evaluati1_1_, clienteval2_.content as content9_1_, clienteval2_.positiveYN as positiveYN9_1_, job3_.serviceId as serviceId2_, job3_.clientPartyId as clientPa2_11_2_, job3_.employeePartyId as employee3_11_2_, job3_.tarifId as tarifId11_2_, job3_.contactPointId as contactP5_11_2_, job3_.fileId as fileId11_2_, job3_.jobTitle as jobTitle11_2_, job3_.jobDescription as jobDescr8_11_2_, job3_.contactDate as contactD9_11_2_, job3_.closeDate as closeDate11_2_, job3_.contractDate as contrac11_11_2_, job3_.babFeedback as babFeed12_11_2_, job3_.comment as comment11_2_, job3_.invoiceDate as invoice14_11_2_, job3_.invoiceNumber as invoice15_11_2_, job3_.invoiceComment as invoice16_11_2_, job3_.otherCharges as otherCh17_11_2_, job3_.otherChargesDescription as otherCh18_11_2_ from t_clientcontact jobs0_ inner join t_service job1_ on jobs0_.serviceId=job1_.serviceId left outer join t_evaluation clienteval2_ on job1_.serviceId=clienteval2_.evaluationId left outer join t_service job3_ on clienteval2_.evaluationId=job3_.serviceId where jobs0_.partyId=?
Hibernate: select contactsfo0_.serviceId as serviceId__, contactsfo0_.partyId as partyId__, contact1_.partyId as partyId0_, contact1_.comment as comment4_0_, contact1_1_.sex as sex6_0_, contact1_1_.title as title6_0_, contact1_1_.maritalStatusCode as maritalS4_6_0_, contact1_1_.birthdate as birthdate6_0_, contact1_1_.firstname as firstname6_0_, contact1_1_.lastname as lastname6_0_ from t_clientcontact contactsfo0_ inner join t_party contact1_ on contactsfo0_.partyId=contact1_.partyId left outer join t_person contact1_1_ on contact1_.partyId=contact1_1_.partyId where contactsfo0_.serviceId=?