Hi Everybody,
I am trying to get one-2-many relationship running with dynamic class feature of hibernate 3.0.
So far i have been able persist, load a single dynamic class (No relation ship) and also i was also able to run the one-2-one relation using dynamic class feature.
But i have run into a problem where i am not able to proceed further experimenting with one-2-many relationship using dynamic class feature.
Below are the sample code, dbscirpt that i am using.
Please do look into my sample code along with the stack trace and let me know what i am missing that i am not able to get the one-2-many relationship running.
I did visit few forums where people have been talking about closed session when loading collection in relationship scenario. My sample code doesn't close the session until i finish iterating and displaying the data on the console. Only after the display process i am closing the session.
I have bee on this problem for quite some time and have run out of option.
Please do put in your thoughts and give it a fresh look and let me know if i have to do anything special to get this one running.
Hibernate version: 3.0
Mapping documents:
CatOwner.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Configuration file for CatOwner. A Cat Owner can be associated with many cat depicting 1-n relationship -->
<hibernate-mapping auto-import="false">
<import class="java.util.Map" rename="CatOwnerPersistentMap"/>
<dynamic-class entity-name="CatOwnerPersistentMap" table="CatOwner" discriminator-value="CO" persister="org.hibernate.persister.SingleTableEntityPersister">
<id name="owner_id" type="string" unsaved-value="null" >
<column name="OWNER_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" type="string"/>
<set name="cats" inverse="true" lazy="false">
<key column="owner_id"/>
<one-to-many class="CatPersistentMap"/>
</set>
</dynamic-class>
</hibernate-mapping>
Cat.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Configuration file for Cat. A Cat can be associated at most one owner -->
<hibernate-mapping auto-import="false">
<import class="java.util.Map" rename="CatPersistentMap"/>
<dynamic-class entity-name="CatPersistentMap" table="Cat" discriminator-value="C" persister="org.hibernate.persister.SingleTableEntityPersister">
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex" type="char"/>
<property name="weight" type="float" />
<many-to-one name="CatOwner" column="owner_id" class="CatOwnerPersistentMap" />
</dynamic-class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
transaction = session.beginTransaction();
String hqlQuery = "from CatOwnerPersistentMap catOwnerPersistentMap";
Query query = session.createQuery(hqlQuery);
List catOwnerList = query.list();
//Iterate over the list and display the objects in the result
Full stack trace of any exception that occurs:
Dec 31, 2004 9:56:13 AM org.hibernate.LazyInitializationException <init>
SEVERE: cannot access loading collection
org.hibernate.LazyInitializationException: cannot access loading collection
at org.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:192)
at org.hibernate.collection.PersistentCollection.read(PersistentCollection.java:72)
at org.hibernate.collection.Set.hashCode(Set.java:307)
at java.util.HashMap$Entry.hashCode(Unknown Source)
at java.util.AbstractMap.hashCode(Unknown Source)
at java.util.HashMap$Entry.hashCode(Unknown Source)
at java.util.AbstractMap.hashCode(Unknown Source)
at java.util.HashMap.hash(Unknown Source)
at java.util.HashMap.put(Unknown Source)
at java.util.HashSet.add(Unknown Source)
at java.util.AbstractCollection.addAll(Unknown Source)
at org.hibernate.collection.Set.endRead(Set.java:237)
at org.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:1430)
at org.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:1417)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:379)
at org.hibernate.loader.Loader.doQuery(Loader.java:361)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:167)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1127)
at org.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:86)
at org.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:419)
at org.hibernate.event.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:53)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:2248)
at org.hibernate.collection.PersistentCollection.forceInitialization(PersistentCollection.java:344)
at org.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:1485)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:172)
at org.hibernate.loader.Loader.doList(Loader.java:1201)
at org.hibernate.loader.Loader.list(Loader.java:1186)
at org.hibernate.hql.QueryTranslatorImpl.list(QueryTranslatorImpl.java:873)
at org.hibernate.impl.SessionImpl.find(SessionImpl.java:812)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:84)
at com.accelrys.platform.test.TestCatOwnerQuery.executeCustomHQL(TestCatOwnerQuery.java:98)
at com.accelrys.platform.test.TestCatOwnerQuery.main(TestCatOwnerQuery.java:54)
Name and version of the database you are using: Oracle 9i
The generated SQL (show_sql=true):
Hibernate: select catownerpe0_.OWNER_ID as OWNER_ID, catownerpe0_.name as name1_ from CatOwner catownerpe0_
Hibernate: select cats0_.owner_id as owner_id__, cats0_.CAT_ID as CAT_ID__, cats0_.CAT_ID as CAT_ID0_, cats0_.NAME as NAME0_0_, cats0_.sex as sex0_0_, cats0_.weight as weight0_0_, cats0_.owner_id as owner_id0_0_ from Cat cats0_ where cats0_.owner_id=?
Debug level Hibernate log excerpt:
Db Schema and Dummy Data:
CREATE TABLE CATOWNER
(
OWNER_ID VARCHAR2(32 BYTE),
NAME VARCHAR2(32 BYTE)
)
INSERT INTO CATOWNER(OWNER_ID, NAME) VALUES ( '2c9ac3ba00e67cc40100e67cc6300001', 'Jeddai');
INSERT INTO CATOWNER(OWNER_ID, NAME) VALUES ( '2c9ac3ba00e67cf90100e67cfb690001', 'Neo');
INSERT INTO CATOWNER(OWNER_ID, NAME) VALUES ( '2c9ac3ba00e67d330100e67d35c20001', 'Mac');
INSERT INTO CATOWNER(OWNER_ID, NAME) VALUES ( '2c9ac3ba00e67d620100e67d64b20001', 'CrazyOwner');
INSERT INTO CATOWNER(OWNER_ID, NAME) VALUES ( '2c9ac3ba00e67d9a0100e67d9c7b0001', 'FoozCat');
INSERT INTO CATOWNER(OWNER_ID, NAME) VALUES ( '2c9ac3ba00e67dc60100e67dc7b20001', 'YoYoCat');
CREATE TABLE CAT
(
CAT_ID VARCHAR2(32 BYTE),
NAME VARCHAR2(16 BYTE),
SEX CHAR(1 BYTE),
WEIGHT FLOAT(63),
OWNER_ID VARCHAR2(32 BYTE)
)
INSERT INTO CAT ( CAT_ID, NAME, SEX, WEIGHT, OWNER_ID) VALUES ( '2c9ac3c6004fea7701004fea7a130001', 'Princess', 'F', 7.4, '2c9ac3ba00e67cc40100e67cc6300001');
INSERT INTO CAT ( CAT_ID, NAME, SEX, WEIGHT, OWNER_ID) VALUES ( '2c9ac3b700827d630100827d65930001', 'Feebie', 'F', 10.3, '2c9ac3ba00e67cf90100e67cfb690001');
INSERT INTO CAT ( CAT_ID, NAME, SEX, WEIGHT, OWNER_ID) VALUES ( '2c9ac3b7008903da01008903dc7d0001', 'Feline-Horror', 'F', 12.2, '2c9ac3ba00e67d330100e67d35c20001');
INSERT INTO CAT ( CAT_ID, NAME, SEX, WEIGHT, OWNER_ID) VALUES ( '2c9ac3b7008953170100895321850001', 'Feline-Critters', 'F', 12.2, '2c9ac3ba00e67d620100e67d64b20001');
INSERT INTO CAT ( CAT_ID, NAME, SEX, WEIGHT, OWNER_ID) VALUES ( '2c9ac3b70089564a010089564bf20001', 'Cat-Vs-Dogs', 'F', 12.2, '2c9ac3ba00e67d9a0100e67d9c7b0001');
INSERT INTO CAT ( CAT_ID, NAME, SEX, WEIGHT, OWNER_ID) VALUES ( '2c9ac3ba00a714810100a7148e7e0001', 'Meow Inc', 'F', 12.2, '2c9ac3ba00e67dc60100e67dc7b20001');
Cheers,
Madhusudan M Krishnamurthy
|