This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate 3: tough time retrieving/persisting child objects

 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read through the documentation but I'm having a tough time understanding the concepts of how to join tables & automatically persist child objects when it comes to related tables. I'm having a particularly hard time getting a simple one-to-one data relationship to end up persisting as two objects.

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.

The two classes are BusinessUnit and Phone and look like this:





The tables are called "business_unit" and "phone". They're straight-forward one-to-one as well...just like the objects - the primary key is a field called "business_unit_id" in business_unit, which is the 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:



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 also tried lazy="false" and got the same result. I tried many-to-one (even though this is never true for this object in this app) and it fails because it tries to treat name="Phone" as a field in the business_unit table when it queries. In the SQL output, nowhere is a join being used as I also would have expected but rather a query is executed for both objects. Never is the setPhone() method of the BusinessUnit class populated w/ data.

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
 
Alwyn Nolte
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use fetch or join for now...

Some tips that might help
1) Use the fully qualified class name for Phone
2) Try:
<one-to-one name="Phone" class="the.classpath.to.Phone"/>
3) In your BusinessUnit class you should have a getter and setter for your Phone class
4) Your query should only return 1 object if you use something like: select b from BusinessUnit as b (or 'from BusinessUnit b'). If you're using a join then 2 objects will be returned (Object[]). You'll have to reference it: <bean:write name="bunit_object" property="bunit_object[1].propertyName"> in your jsp
5) When you've got only 1 object use: bunit_object.propertyName


Send you're query if you don't get it sorted out

Hth
Alwyn
 
Alwyn Nolte
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correction on point number 5...

When you've got only 1 object use: bunit_object.propertyName

should read...

When you've got only 1 object use: phone_object.propertyName

same goes for point number 4...
<bean:write name="bunit_object" property="bunit_object[1].propertyName">

should read...

<bean:write name="bunit_object" property="phone_object[1].propertyName">
[ September 27, 2005: Message edited by: Alwyn Nolte ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic