Hello everybody,
I have the following parent/child hierachy in an single-user standalone application:
Equipment.Calibration.CalibrationPoint.UncertaintySourcesCollection.UncertaintySource
I adopted the detached object strategy and, also, I use cascade="all-delete-orphan" in all associations (one-to-many
always). So, whenever the user change something in any instance of the above classes
I call saveOrUpdate(Equipment) in a new session and all works fine.
The class UncertaintySource is abstract and one of its implementations (TypeImportedUncertaintySource)
can hold references to instances of Calibration, CalibrationPoint or UncertaintySourcesCollection.
And here lies my problem. As I try to do a criteria search for Equipments from the DB, hibernate throws
"ERROR LazyInitializationException:19 - cannot access loading collection" when either Calibration,
CalibrationPoint or UncertaintySourcesCollection is accessed. It happens at the following line of code:
Code:
...
for (ListIterator i = cp.getUncertaintySourcesCollections().listIterator(); i.hasNext();) {
...
OBS: I tried to reattach 'cp' in a new session, but I got an error saying its dangerous to commit while flushing.
I wonder if the problem is that hibernate cannot initialize my hierarchy from a child element (CalibrationPoint
for instance) and only works when I load it from the topmost parent (Equipment). Is this true? If so, any ideas on
how cicumvent it?
Thanks in advance.
Hibernate version: 3.0.3
Name and version of the database you are using: HSQLDB 1.7.3
Mapping documents: All mappings declare these id and version tags:
Code:
<id name="id" type="long" column="CLASS_ID">
<generator class="native"/>
</id>
<version name="version" type="long"/>
Class mappings:
Code:
<class name="Equipment" table="EQUIPMENTS">
<list name="calibrations" table="EQUIP_CALIBRATIONS" inverse="true" lazy="false" cascade="all-delete-orphan">
<key column="EQUIP_ID"/>
<list-index column="IDX"/>
<one-to-many class="Calibration"/>
</list>
</class>
<class name="Calibration" table="CALIBRATIONS">
<many-to-one name="parent" class="Equipment" column="EQUIP_ID" not-null="true"/>
<property name="index" column="IDX"/>
<list name="calibrationPoints" table="CALIB_CALIBPOINTS" inverse="true" lazy="false" cascade="all-delete-orphan">
<key column="CALIB_ID"/>
<list-index column="IDX"/>
<one-to-many class="CalibrationPoint"/>
</list>
</class>
<class name="CalibrationPoint" table="CALIBRATION_POINTS">
<many-to-one name="parent" class="Calibration" column="CALIB_ID" not-null="true"/>
<property name="index" column="IDX"/>
<many-to-one name="mainUncertaintySourcesCollection" class="UncertaintySourcesCollection" column="MAIN_USCOLL_ID" lazy="false" unique="true"/>
<list name="uncertaintySourcesCollections" table="CALIBPOINT_USCOLL" lazy="false" inverse="true" cascade="all-delete-orphan">
<key column="CP_ID"/>
<list-index column="IDX"/>
<one-to-many class="UncertaintySourcesCollection"/>
</list>
</class>
<class name="UncertaintySourcesCollection" table="UNCERTAINTY_SOURCES_COLLECTIONS">
<many-to-one name="parent" class="CalibrationPoint" column="CP_ID" not-null="true"/>
<property name="index" column="IDX"/>
<list name="uncertaintySources" table="USCOLL_USES" lazy="false" inverse="true" cascade="all-delete-orphan">
<key column="USCOLL_ID"/>
<list-index column="IDX"/>
<one-to-many class="UncertaintySource"/>
</list>
</class>
<class name="UncertaintySource" table="UNCERTAINTY_SOURCES">
<many-to-one name="parent" class="UncertaintySourcesCollection" column="USCOLL_ID" not-null="true"/>
<property name="index" column="IDX"/>
...
<property name="sensitivityCoefficientFormula" column="SENSITIVITY_COEFFICIENT_FORMULA"/>
<joined-subclass name="TypeBUncertaintySource" table="TYPEB_UNCERTAINTY_SOURCES">
<key column="US_ID"/>
<joined-subclass name="TypeImportedUncertaintySource" table="TYPEI_UNCERTAINTY_SOURCES">
<key column="US_ID"/>
<property name="choosenUeStrategy" column="UE_STRATEGY"/>
<many-to-one name="calibration" class="Calibration" lazy="false" column="CALIBRATION_ID" unique="true"/>
<many-to-one name="calibrationPoint" class="CalibrationPoint" lazy="false" column="CP_ID" unique="true"/>
<many-to-one name="uncertaintySourcesCollection" class="UncertaintySourcesCollection" lazy="false" column="USCOLL_ID" unique="true"/>
</joined-subclass>
</joined-subclass>
<joined-subclass name="TypeAUncertaintySource" table="TYPEA_UNCERTAINTY_SOURCES">
...
</joined-subclass>
</class>
Code between sessionFactory.openSession() and session.close():Code:
...
for (ListIterator i = cp.getUncertaintySourcesCollections().listIterator(); i.hasNext();) {
...
OBS: cp is an instance of CalibrationPoint
Full stack trace of any exception that occurs:
Opening new Session for this thread.
Starting new database transaction in this thread.
Hibernate: /*criteria query*/select top ? this_.EQUIP_ID as EQUIP1_0_, this_.version as version0_0_, this_.code as code0_0_, this_.type as type0_0_, this_.name as name0_0_, this_.maker as maker0_0_, this_.model as model0_0_, this_.serialNumber as serialNu8_0_0_, this_.precision as precision0_0_, this_.scope as scope0_0_, this_.description as descrip11_0_0_ from EQUIPMENTS this_ order by this_.code asc
Hibernate: /*load one-to-many com.estatcamp.sge.domain.Equipment.calibrations*/select calibratio0_.EQUIP_ID as EQUIP3___, calibratio0_.CALIB_ID as CALIB1___, calibratio0_.IDX as IDX__, calibratio0_.CALIB_ID as CALIB1_0_, calibratio0_.version as version1_0_, calibratio0_.EQUIP_ID as EQUIP3_1_0_, calibratio0_.IDX as IDX1_0_, calibratio0_.date as date1_0_, calibratio0_.expirationDate as expirati6_1_0_, calibratio0_.quantity as quantity1_0_, calibratio0_.unit as unit1_0_, calibratio0_.comments as comments1_0_ from CALIBRATIONS calibratio0_ where calibratio0_.EQUIP_ID=?
Hibernate: /*load one-to-many com.estatcamp.sge.domain.Calibration.calibrationPoints*/select calibratio0_.CALIB_ID as CALIB3___, calibratio0_.CP_ID as CP1___, calibratio0_.IDX as IDX__, calibratio0_.CP_ID as CP1_0_, calibratio0_.version as version2_0_, calibratio0_.CALIB_ID as CALIB3_2_0_, calibratio0_.IDX as IDX2_0_, calibratio0_.COVERAGE_FACTOR as COVERAGE5_2_0_, calibratio0_.EXPANDED_UNCERTAINTY as EXPANDED6_2_0_, calibratio0_.COMMENTS as COMMENTS2_0_, calibratio0_.MAIN_USCOLL_ID as MAIN8_2_0_ from CALIBRATION_POINTS calibratio0_ where calibratio0_.CALIB_ID=?
Hibernate: /*load one-to-many com.estatcamp.sge.domain.CalibrationPoint.uncertaintySourcesCollections*/select uncertaint0_.CP_ID as CP3___, uncertaint0_.USCOLL_ID as USCOLL1___, uncertaint0_.IDX as IDX__, uncertaint0_.USCOLL_ID as USCOLL1_0_, uncertaint0_.version as version3_0_, uncertaint0_.CP_ID as CP3_3_0_, uncertaint0_.IDX as IDX3_0_, uncertaint0_.NAME as NAME3_0_, uncertaint0_.DESCRIPTION as DESCRIPT6_3_0_ from UNCERTAINTY_SOURCES_COLLECTIONS uncertaint0_ where uncertaint0_.CP_ID=?
Hibernate: /*load one-to-many com.estatcamp.sge.domain.UncertaintySourcesCollection.uncertaintySources*/select uncertaint0_.USCOLL_ID as USCOLL3___, uncertaint0_.US_ID as US1___, uncertaint0_.IDX as IDX__, uncertaint0_.US_ID as US1_0_, uncertaint0_.version as version4_0_, uncertaint0_.USCOLL_ID as USCOLL3_4_0_, uncertaint0_.IDX as IDX4_0_, uncertaint0_.NAME as NAME4_0_, uncertaint0_.DESCRIPTION as DESCRIPT6_4_0_, uncertaint0_.ESTIMATE as ESTIMATE4_0_, uncertaint0_.ESTIMATE_UNIT as ESTIMATE8_4_0_, uncertaint0_.MEASURE as MEASURE4_0_, uncertaint0_.DISTRIBUTION as DISTRIB10_4_0_, uncertaint0_.DIVIDER as DIVIDER4_0_, uncertaint0_.SENSITIVITY_COEFFICIENT_FORMULA as SENSITI12_4_0_, uncertaint0_.DEGREES_OF_FREEDOM as DEGREES13_4_0_, uncertaint0_2_.UE_STRATEGY as UE2_6_0_, uncertaint0_2_.CALIBRATION_ID as CALIBRAT3_6_0_, uncertaint0_2_.CP_ID as CP4_6_0_, uncertaint0_2_.USCOLL_ID as USCOLL5_6_0_, case when uncertaint0_2_.US_ID is not null then 2 when uncertaint0_3_.US_ID is not null then 3 when uncertaint0_5_.US_ID is not null then 5 when uncertaint0_1_.US_ID is not null then 1 when uncertaint0_4_.US_ID is not null then 4 when uncertaint0_.US_ID is not null then 0 end as clazz_0_ from UNCERTAINTY_SOURCES uncertaint0_ left outer join TYPEB_UNCERTAINTY_SOURCES uncertaint0_1_ on uncertaint0_.US_ID=uncertaint0_1_.US_ID left outer join TYPEI_UNCERTAINTY_SOURCES uncertaint0_2_ on uncertaint0_.US_ID=uncertaint0_2_.US_ID left outer join TYPEH_UNCERTAINTY_SOURCES uncertaint0_3_ on uncertaint0_.US_ID=uncertaint0_3_.US_ID left outer join TYPEA_UNCERTAINTY_SOURCES uncertaint0_4_ on uncertaint0_.US_ID=uncertaint0_4_.US_ID left outer join TYPEAH_UNCERTAINTY_SOURCES uncertaint0_5_ on uncertaint0_.US_ID=uncertaint0_5_.US_ID where uncertaint0_.USCOLL_ID=?
15:29:47,703 ERROR LazyInitializationException:19 - cannot access loading collection
org.hibernate.LazyInitializationException: cannot access loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:167)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:47)
at org.hibernate.collection.PersistentList.listIterator(PersistentList.java:266)
at com.estatcamp.sge.domain.UncertaintySource.getUsSet(UncertaintySource.java:390)
at com.estatcamp.sge.domain.UncertaintySource.setSensitivityCoefficientFormula(UncertaintySource.java:445)
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:324)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:40)
at org.hibernate.tuple.AbstractTuplizer.setPropertyValues(AbstractTuplizer.java:207)
at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:171)
at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2922)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:492)
at org.hibernate.loader.Loader.doQuery(Loader.java:411)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:206)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1360)
at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:107)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1353)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:170)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:47)
at org.hibernate.collection.PersistentList.listIterator(PersistentList.java:266)
at com.estatcamp.sge.domain.UncertaintySourcesCollection.setUncertaintySources(UncertaintySourcesCollection.java:351)
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:324)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:40)
at org.hibernate.tuple.AbstractTuplizer.setPropertyValues(AbstractTuplizer.java:207)
at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:171)
at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2922)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:492)
at org.hibernate.loader.Loader.doQuery(Loader.java:411)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:206)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1360)
at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:107)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1353)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:270)
at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(PersistenceContext.java:745)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:211)
at org.hibernate.loader.Loader.doList(Loader.java:1515)
at org.hibernate.loader.Loader.list(Loader.java:1498)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1253)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)
at com.estatcamp.sge.gui.EquipmentSearchPanel.searchEquipment(EquipmentSearchPanel.java:263)
at com.estatcamp.sge.gui.EquipmentSearchPanel.access$4(EquipmentSearchPanel.java:224)
at com.estatcamp.sge.gui.EquipmentSearchPanel$2.actionPerformed(EquipmentSearchPanel.java:123)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:141)
at java.awt.Dialog$1.run(Dialog.java:540)
at java.awt.Dialog.show(Dialog.java:561)
at java.awt.Component.show(Component.java:1133)
at java.awt.Component.setVisible(Component.java:1088)
at com.estatcamp.sge.gui.EquipmentSearchPanel.showDialog(EquipmentSearchPanel.java:164)
at com.estatcamp.sge.gui.EquipmentEditor.openEquipment(EquipmentEditor.java:547)
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:324)
at com.estatcamp.sge.gui.GenericAction.actionPerformed(GenericAction.java:85)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at javaUncertaintySource.getUsSet(): threw HibernateException
class com.estatcamp.sge.domain.TypeImportedUncertaintySource.setSensitivityCoefficientFormula() -> RuntimeException.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
Hibernate: /*load one-to-many com.estatcamp.sge.domain.Equipment.calibrations*/select calibratio0_.EQUIP_ID as EQUIP3___, calibratio0_.CALIB_ID as CALIB1___, calibratio0_.IDX as IDX__, calibratio0_.CALIB_ID as CALIB1_0_, calibratio0_.version as version1_0_, calibratio0_.EQUIP_ID as EQUIP3_1_0_, calibratio0_.IDX as IDX1_0_, calibratio0_.date as date1_0_, calibratio0_.expirationDate as expirati6_1_0_, calibratio0_.quantity as quantity1_0_, calibratio0_.unit as unit1_0_, calibratio0_.comments as comments1_0_ from CALIBRATIONS calibratio0_ where calibratio0_.EQUIP_ID=?