-->
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.  [ 4 posts ] 
Author Message
 Post subject: tough time w/ "one-to-one" and "join"
PostPosted: Tue Sep 20, 2005 5:54 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
I've read through the documentation but I'm having a tough time understanding the concepts of how to join tables & persist objects when it comes to related tables.

I have a very good understanding of relational databases & table relationships and I understand associations between business objects...it's Hibernate's XML mapping to make these things work that's confusing me since I'm used to writing DAO code myself.

I've got two classes that I need to populate values from two tables which have a one-to-one relationship between them.

The classes are BusinessUnit and Phone and look like this:

Code:
public class Phone
{
   private int _id;
   private int _employeeID;
   private int _businessUnitID;
   private String _phoneType;
   private String _phoneNumber;
   private String _extension;

   public Phone()
   {
   }

   public int getBusinessUnitID()
   {
      return this._businessUnitID;
   }

   public void setBusinessUnitID(int unitID)
   {
      this._businessUnitID = unitID;
   }

   public int getEmployeeID()
   {
      return this._employeeID;
   }

   public void setEmployeeID(int employeeid)
   {
      this._employeeID = employeeid;
   }

   public int getID()
   {
      return this._id;
   }

   public void setID(int id)
   {
      this._id = id;
   }

   public String getPhoneNumber()
   {
      return this._phoneNumber;
   }

   public void setPhoneNumber(String number)
   {
      this._phoneNumber = number;
   }

   public String getPhoneType()
   {
      return this._phoneType;
   }

   public void setPhoneType(String type)
   {
      this._phoneType = type;
   }

   public String getExtension()
   {
      return this._extension;
   }

   public void setExtension(String extension)
   {
      this._extension = extension;
   }
}


Code:
public class BusinessUnit
{
   private int _id;
   private int _divisionNo;
   private String _name;
   private IPhone _phone;

   public int getDivisionNo()
   {
      return this._divisionNo;
   }

   public void setDivisionNo(int no)
   {
      this._divisionNo = no;
   }

   public int getID()
   {
      return this._id;
   }

   public void setID(int id)
   {
      this._id = id;
   }

   public String getName()
   {
      return this._name;
   }

   public void setName(String name)
   {
      this._name = name;
   }

   public IPhone getPhone()
   {
      return this._phone;
   }

   public void setPhone(IPhone phone)
   {
      this._phone = phone;
   }
}


The tables are called "business_unit" and "phone". They're straight-forward one-to-one as well...just like the objects - they key is a field called "business_unit_id" which is a foreign key in the phone table.

Before this is dismissed as bad design and someone tells me to denormalize the phone info into the business_unit table - this is a requirement. The phone table is joined to other tables as other entities have phone numbers as well, not just business units.

I tried this in my BusinessUnit.hbm.xml file:

Code:
<hibernate-mapping>
   <class name="com.agribeef.bol.BusinessUnit" table="ed_business_unit">
     <id name="ID" column="business_unit_id" type="int" unsaved-value="0">
           <generator class="identity"/>
    </id>
     <property name="Name" column="name"/>
     <property name="DivisionNo" column="division_number"/>
     <one-to-one
        name="Phone"
        class="Phone"
        fetch="join"
        lazy="true" />
   </class>
</hibernate-mapping>


When I run the app I see two separate queries being executed, one for the business_unit table and one for the phone table...but the phone field has no value. I was hoping to see the Phone object in the BusinessUnit class populated w/ the data from the phone query (one-to-one relationship).

I've been poring over the documentation for half the day but I'm not sure exactly what I need to get this to work right...can someone nudge me along?

Thanks!

-v

Hibernate version: 3.0.5

Name and version of the database you are using: MS SQL Server 2000


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 9:33 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
try setting lazy='false'
Code:
     <one-to-one
        name="Phone"
        class="Phone"
        fetch="join"
        lazy="false" />


and post your query code


Top
 Profile  
 
 Post subject: no change
PostPosted: Wed Sep 21, 2005 10:24 am 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
With lazy=false....the query behavior was exactly the same:

Code:
Hibernate: select businessun0_.business_unit_id as business1_, businessun0_.name as name5_, businessun0_.division_number as division3_5_ from business_unit businessun0_

Hibernate: select phone0_.phone_number_id as phone1_0_, phone0_.employee_id as employee2_3_0_, phone0_.business_unit_id as business3_3_0_, phone0_.phone_type as phone4_3_0_, phone0_.phone_number as phone5_3_0_, phone0_.extension as extension3_0_ from phone phone0_ where phone0_.phone_number_id=?


As you can see, the phone info is queried at the same time as the business unit data...but the Phone "setter" isn't set in the BusinessUnit object.

I've seen "many-to-one" suggested for this in some documentation but I'd really like it to be strictly one-to-one (in both directions w/ other data too.)


Top
 Profile  
 
 Post subject: also...
PostPosted: Wed Sep 21, 2005 12:57 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
I also tried a many-to-one (even though this is never true of this object in this particular application.) It causes an error because the query is malformed, treating the name="Phone" as a field in the business_unit. Nowhere am I seeing a join in the query, as I would have expected, even when I explicitly state fetch="join".


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.