HI,
I have a small query. Although I have set the outer-join property of user in the Child class as "true", but when I retrieve the children of the Parent class it fails to eagerly fetch the user elements associated with the Child class. Can anyone help me with this?
Hibernate version:3.0
[b]Mapping documents:
Code:
[color=green]<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.parentChild">
<class name="Parent">
<id name="id" column="PARENT_ID">
<generator class="increment" />
</id>
<version name="version" type="long" />
<property name="type" />
<set name="children" cascade="all-delete-orphan" inverse="true">
<key column="parent_id" />
<one-to-many class="Child" />
</set>
</class>
<class name="Child">
<id name="id" column="CHILD_ID">
<generator class="increment" />
</id>
<property name="schoolName" type="string" />
<many-to-one name="user" column="USER_ID" class="Usr" cascade="all" [b]outer-join="true"[/b] />
</class>
<class name="Usr" table="USR">
<id name="id" column="USER_ID">
<generator class="increment" />
</id>
<property name="firstName" type="string" column="firstName" not-null="true" />
<property name="lastName" type="string" column="lastName" not-null="false" />
</class>
</hibernate-mapping>[/color]
Code between sessionFactory.openSession() and session.close():package org.hibernate.parentChild;
Code:
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.auction.HibernateUtil;
import org.hibernate.auction.User;
public class ParentManager {
private Parent theParent;
private void addParent() throws Exception {
System.out.println("adding Parent ........");
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
theParent = new Parent();
Usr user = new Usr();
user.setFirstName("first child's first name");
user.setLastName("first child's last name");
Child child = new Child();
child.setUser(user);
child.setSchoolName("first child's school name");
theParent.addChild(child);
child = new Child();
user = new Usr();
user.setFirstName("second child's first name");
user.setLastName("second child's last name");
child.setUser(user);
child.setSchoolName("second child's school name");
theParent.addChild(child);
s.save(theParent);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw e;
} finally {
HibernateUtil.closeSession();
}
}
private void getChildren() throws Exception{
System.out.println("getting children......");
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
List l = s.createQuery("from Child").list();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
throw e;
}
}
public static void main(String[] args) throws Exception {
final ParentManager test = new ParentManager();
test.addParent();
test.getChildren();
}
}
The generated SQL (show_sql=true):
Hibernate: insert into Parent (version, type, PARENT_ID) values (?, ?, ?)
Hibernate: insert into USR (firstName, lastName, USER_ID) values (?, ?, ?)
Hibernate: insert into Child (schoolName, USER_ID, CHILD_ID) values (?, ?, ?)
Hibernate: insert into USR (firstName, lastName, USER_ID) values (?, ?, ?)
Hibernate: insert into Child (schoolName, USER_ID, CHILD_ID) values (?, ?, ?)
getting children......
Hibernate: select child0_.CHILD_ID as CHILD1_, child0_.schoolName as schoolName4_, child0_.USER_ID as USER3_4_ from Child child0_
Thanks in advance,
Ashish Abrol