I've got a problem when trying to replace <set> with <list> in the hbm.xml describing
bi-directional relation (one-to-many). I've got a list of 8 children instead of 4 (as it should be). Half of those 8 are nulls.
I'm using:
hibernate-3.2.4 SP1
mysql 5
java 5
My java code:
In main():
Customer load =
(Customer)b.getLazySets("from persist.domain.Customer _o where _o.custId = " + 1);
Then:
protected Object getLazySets(String sql) {
Session s = HibernateUtil.currentSession();
s.beginTransaction();
Object rec = s.createQuery(sql).uniqueResult();
populateLazySet(rec);
//Query q = s.createQuery("select name, phoneNumber from persist.domain.Customer");
s.getTransaction().commit();
return rec;
}
private void populateLazySet(Object rec) {
protected Object getLazySets(String sql) {
Session s = HibernateUtil.currentSession();
s.beginTransaction();
Object rec = s.createQuery(sql).uniqueResult();
populateLazySet(rec);
//Query q = s.createQuery("select name, phoneNumber from persist.domain.Customer");
s.getTransaction().commit();
return rec;
}
private void populateLazySet(Object rec) {
log.debug("" + rec.getClass());
try {
Class<?> c = rec.getClass();
Method[] mm = c.getMethods();
log.debug("Methods length: " + mm.length);
for (Method m: mm) {
if (m.getReturnType().getName().equals("java.util.List")) {
Object h = m.invoke(rec, new Object[0]);
log.debug(m.getName() + " returned: " + h);
}
// log.debug(m.getName()+" "+ m.getReturnType());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Log:
Hibernate: select customer0_.Cust_ID as Cust1_0_, customer0_.Name as Name0_, customer0_.Phone_Number as Phone3_0_, customer0_.Street_Address as Street4_0_, customer0_.City as City0_, customer0_.State as State0_ from vlad.customers customer0_ where customer0_.Cust_ID=1
Source breakpoint occurred at line 114 of BaseDAO.java.
14:51:05,937 DEBUG BaseDAO:184 - class persist.domain.Customer
14:51:05,937 DEBUG BaseDAO:188 - Methods length: 23
Hibernate: select customeror0_.CustomerId as CustomerId1_, customeror0_.CustomerOrderId as Customer1_1_, customeror0_.customerOrderId as customer1_1_, customeror0_.CustomerOrderId as Customer1_1_0_, customeror0_.CustomerId as CustomerId1_0_, customeror0_.OrderNumber as OrderNum3_1_0_, customeror0_.Description as Descript4_1_0_, customeror0_.Price as Price1_0_, customeror0_.OrderDate as OrderDate1_0_, customeror0_.ShippingDate as Shipping7_1_0_ from vlad.customerorder customeror0_ where customeror0_.CustomerId=?
Hibernate: select customer0_.Cust_ID as Cust1_0_, customer0_.Name as Name0_, customer0_.Phone_Number as Phone3_0_, customer0_.Street_Address as Street4_0_, customer0_.City as City0_, customer0_.State as State0_ from vlad.customers customer0_ where customer0_.Cust_ID=1
Source breakpoint occurred at line 114 of BaseDAO.java.
14:51:05,937 DEBUG BaseDAO:184 - class persist.domain.Customer
14:51:05,937 DEBUG BaseDAO:188 - Methods length: 23
Hibernate: select customeror0_.CustomerId as CustomerId1_, customeror0_.CustomerOrderId as Customer1_1_, customeror0_.customerOrderId as customer1_1_, customeror0_.CustomerOrderId as Customer1_1_0_, customeror0_.CustomerId as CustomerId1_0_, customeror0_.OrderNumber as OrderNum3_1_0_, customeror0_.Description as Descript4_1_0_, customeror0_.Price as Price1_0_, customeror0_.OrderDate as OrderDate1_0_, customeror0_.ShippingDate as Shipping7_1_0_ from vlad.customerorder customeror0_ where customeror0_.CustomerId=?
14:51:06,062 DEBUG BaseDAO:192 - getCustomerOrders returned: [null, persist.domain.CustomerOrder@bdb375, null, persist.domain.CustomerOrder@c19fbf, null, persist.domain.CustomerOrder@1ded4c9, null, persist.domain.CustomerOrder@63e563]
My mappings:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 14, 2007 1:33:36 PM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="persist.domain.Customer" table="customers" catalog="vlad">
<comment></comment>
<id name="custId" type="integer">
<column name="Cust_ID" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="Name" length="30">
<comment></comment>
</column>
</property>
... more of simple properties...
<list name="customerOrders" inverse="true" lazy="true">
<key>
<column name="CustomerId" not-null="true">
<comment></comment>
</column>
</key>
<index column="customerOrderId"/>
<one-to-many class="persist.domain.CustomerOrder" />
</list>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 14, 2007 1:33:36 PM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="persist.domain.CustomerOrder" table="customerorder" catalog="vlad">
<comment></comment>
<id name="customerOrderId" type="integer">
<column name="CustomerOrderId" />
<generator class="assigned" />
</id>
<many-to-one name="customers" class="persist.domain.Customer" fetch="select">
<column name="CustomerId" not-null="true">
<comment></comment>
</column>
</many-to-one>
...other simple properties
</class>
</hibernate-mapping>
_________________ vlad
|