These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: problem with outer-join
PostPosted: Tue Apr 03, 2007 2:19 am 
Newbie

Joined: Tue Apr 03, 2007 2:10 am
Posts: 2
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


Top
 Profile  
 
 Post subject: Lazy Initialization
PostPosted: Tue Apr 03, 2007 3:40 am 
Beginner
Beginner

Joined: Mon Nov 22, 2004 11:21 am
Posts: 42
Hello Ashish,
Hibernate uses lazy initialization for referenced objects by default. If you wish to change this behaviour, you have to turn it off. I recommend to turn it off only where absolutely needed, otherwise you may fetch a very large subset of your database with just one simply query.

In your example:
Code:
<many-to-one name="user" column="USER_ID" class="Usr" cascade="all" outer-join="true" lazy="false"/>
Regards,
Georg

_________________
Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 4:26 am 
Newbie

Joined: Tue Apr 03, 2007 2:10 am
Posts: 2
Yes with Lazy="false" I am able to retrieve the User object also but does that mean whereever I have to use outer-join="true" then alzy must be set to false. Also by setting lazy ="false" and outer-join="true" I am achieving the same thing that could have been achieved with lazy ="false". Then what is the purpose of outer-join


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 5:29 am 
Beginner
Beginner

Joined: Mon Nov 22, 2004 11:21 am
Posts: 42
Hello Ashish,

sorry, I had ignored your setting outer-join="true" because I never used it. By the way, is it the same as fetch="join"? I didn't find any documentation for a feature called outer-join="true". If you use lazy="false" you can specify whether one SQL is fired (fetch="join) or 2 sequential SQL's are fired (fetch="select") for initializing both objects.

Regards,
Georg

_________________
Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 7:23 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 9:07 am
Posts: 27
My query still remains unanswered.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.