Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I'm running a JUnit test case that is attemping to call a query using HQL and createQuery() method. The query successfully returns a PERSON record but fails to return the assoicated ADDRESS record. I'm using lazy intialization and the associated collection appears to be intialized but the resulting collection has a 0 size. My data set should have associated address data for each person.
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();
Hibernate.initialize(set);
assertTrue(Hibernate.isInitialized(set));
this works
Iterator iterator = set.iterator();
int counter = 0;
while (iterator.hasNext()) {
counter++;
Address address = (Address) iterator.next();
}
assertEquals(counter,3);
this fails has 0 count
}
}
persondao.closeSession();
Full stack trace of any exception that occurs:
Only exception is an assertion failure
expected:<0> but was:<3>" type="org.apache.cactus.internal.client.AssertionFailedErrorWrapper">junit.framework.AssertionFailedError: expected:<0> but was:<3> at com.everware.component.unittest.person.PersonTestCaseWrapper.testSearchByFullName1
Name and version of the database you are using:
Oracle 9.0.1
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
16:43:51,826 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_.SURNAME as 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=?
16:43:51,856 DEBUG SQL:346
select
addresss0_.ADDRESSID as ADDRESSID1_,
addresss0_.ADDRESSID as ADDRESSID1_0_,
addresss0_.address1 as address2_1_0_,
addresss0_.address2 as address3_1_0_,
addresss0_.city as city1_0_,
addresss0_.state as state1_0_,
addresss0_.zip as zip1_0_,
addresss0_.InstanceID as InstanceID1_0_
from Address addresss0_
where addresss0_.ADDRESSID=?
Note both PERSON and ADDRESS queries execute here.
After this the deletes from the tear down start.