Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hi:
I'm running a JUnit test case that is attemping to call a query using HQL. The query successfully returns a PERSON record but fails to return the assoicated ADDRESS record. I'm using lazy intialization but the associated collection does not appear to be intialized.
Hibernate version:
3.1.2
Mapping documents:
-------------------------------- Person --------------------------------
<hibernate-mapping>
<class name="com.everware.component.unittest.person.Person" table="Person">
<id column="InstanceID" name="instanceID" type="integer" unsaved-value="null">
<generator class="native" />
</id>
<property column="ID_FORMAT_CODE" insert="true" name="idFormatCode" not-null="false" type="string" unique="false" update="true" />
...
<set cascade="none" inverse="true" name="Addresss">
<key column="ADDRESSID" />
<one-to-many class="com.everware.component.unittest.person.Address" />
</set>
...
</class>
</hibernate-mapping>
---------------------------------- Address----------------------------------
<hibernate-mapping>
<class name="com.everware.component.unittest.person.Address" table="Address">
<id column="ADDRESSID" name="addressID" type="integer" unsaved-value="null">
<generator class="native" />
</id>
<property column="address1" insert="true" name="address1" not-null="false" type="string" unique="false" update="true" />
...
<many-to-one cascade="none" class="com.everware.component.unittest.person.Person" name="Person" lazy="proxy">
also tried lazy="true"
<column name="InstanceID" />
</many-to-one>
...
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Session s = persondao.getSession();
String[] type = {"firstName","surname"};
Object[] obj = {"john", "red" };
List l = persondao.searchByFullName(s,obj,type);
executes the query logged in the debug section below
int sizeList = l.size();
assertEquals(sizeList,new Integer(1).intValue());
This works
for (int i=0;i<l.size();i++) {
Person person = (Person) l.get(i);
assertEquals(person.getSurname(),"red");
this works
Set set = person.getAddresss();
assertTrue(Hibernate.isInitialized(set)); this failes
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Address address = (Address) iterator.next();
}
}
assertEquals(set.size(),3);
persondao.closeSession();
Full stack trace of any exception that occurs:
The only exception occurs is an junit assertion failure that the "set" is not intialized
failure message="null" type="org.apache.cactus.internal.client.AssertionFailedErrorWrapper">junit.framework.AssertionFailedError.
If I remove this and simply do a
assertEquals(set.size(),3);
No exceptions occur.. I just get a 0 size.
Name and version of the database you are using:
Oracle 9.0.1
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
15:14:37,036 DEBUG SQL:346 -
select person0_.InstanceID as InstanceID0_,
person0_.ID_FORMAT_CODE as ID2_0_,
person0_.RELIGION_CODE as RELIGION3_0_,
person0_.OBSOLETE_IND as OBSOLETE4_0_,
person0_.NICKNAME as NICKNAME0_,
person0_.CREATED_USERID as CREATED6_0_, person0_.PREVIOUS_SURNAME as PREVIOUS7_0_,
person0_.INCOME_AMT as INCOME8_0_, person0_.LANGUAGE_PREF_CODE as LANGUAGE9_0_,
person0_.SURNAMEas SURNAME0_,
person0_.FIRST_NAME as FIRST11_0_,
person0_.DATE_OF_BIRTH as DATE12_0_,
person0_.salutation as salutation0_,
person0_.ID_VALUE as ID14_0_,
person0_.MARITAL_STATUS_CODE as MARITAL15_0_, person0_.LAST_UPDATED_TS as LAST16_0_,
person0_.DATE_OF_DEATH as DATE17_0_,
person0_.GENDER_CODE as GENDER18_0_,
person0_.CREATED_TS as CREATED19_0_,
person0_.ETHNICITY_CODE as ETHNICITY20_0_,
person0_.LAST_UPDATED_USERID as LAST21_0_, person0_.NAME_SUFFIX_CODE as NAME22_0_,
person0_.ID_Type_Code as ID23_0_,
person0_.INCOME_FREQ_CODE as INCOME24_0_,
person0_.MIDDLE_NAME as MIDDLE25_0_,
person0_.socialSecurityNum as socialS26_0_
from Person person0_
where person0_.FIRST_NAME=? and person0_.SURNAME=?
after this statement the deletes kick off from the "tearDown()" method so it appears that a "SELECT" for the "Address" association never happens