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