-->
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: mapping a <join> where the right-hand table is parent
PostPosted: Wed Jul 05, 2006 1:59 pm 
Hibernate version: 3.1.1

I have a mapping problem with a legacy db where there are two tables to be represented by one class.

It's a one-to-one relationship and the 2 tables were related as a parent-child where my A_LEFT is the child with the foreign key. Unfortunately Hibernate seems to want A_LEFT to be the parent and cannot handle this.

This is the SQL I want:

Code:
select * from A_LEFT A left outer join B_RIGHT B on A.foreignKey = B.id


because B sometimes has no data.

My 1st mapping attempt failed:

Code:
<class table="A_LEFT">
  <id column="id"/>
  <property name="fk" column="A_FK"/>
  <join table="B_RIGHT" optional="true">
    <key column="id" />
  </join>
</class>


because Hibernate expects B to relate to A's primary key. So I looked further and found the <properties> and <property-ref> syntax, which I set up so:

Code:
<class table="A_LEFT">
  <id column="id"/>
  <properties name="aForeignKey">
    <property name="fk" column="A_FK"/>
  </properties>
  <join table="B_RIGHT" optional="true">
    <key column="id" property-ref="aForeignKey"/>
  </join>
</class>


But this failed too, inexplicably. The SQL that it pumps out just completely ignores my <property-ref>, i.e.

Code:
select * from A_LEFT A left outer join B_RIGHT B on A.ID = B.ID


instead of

Code:
select * from A_LEFT A left outer join B_RIGHT B on A.A_FK = B.ID


Why does Hibernate ignore the property-ref on my join key? Could it be a bug? Is there some other approach?


Thanks


Top
  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 3:26 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 2:43 pm
Posts: 29
Location: Silicon Valley
An alternative approach would be to map A and B as separate classes, with a one-to-one relationship. If you really want to make it behave like a single class, put getters and setters on A that delegate to the contained instance of B.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 5:21 am 
Absolutely (or I could get the DBA to write a view) and in fact that is what I am doing in the meantime, but the object model would be cleaner if Hibernate were to allow the <join> to function the other way around. Perhaps I can log that as an enhancement request.

What surprises me though is that the <properties> and <property-ref> in combination with <join> just doesn't work, and without any warnings or complaints. I can't find any Hibernate documentation to suggest that this isn't meant to work, so unless someone knows otherwise, I would log it as a bug.

Getting this mapping to work would be highly advantageous, since the object model which is currently mapped to a legacy database will be re-mapped at some point when I win my struggle to get the schema refactored :)


Top
  
 
 Post subject: log this as a bug?
PostPosted: Mon Jul 10, 2006 11:26 am 
It seems that no-one is using property-refs with <join>, judging from the silence on this issue.

Surely such a mapping technique for a legacy schema is quite common? Perhaps not.

Shall I submit this as a bug?


Top
  
 
 Post subject: found an old bug in JIRA
PostPosted: Mon Jul 10, 2006 12:34 pm 
It's not a issue, it's a TODO according to this rejected JIRA issue

http://opensource.atlassian.com/projects/hibernate/browse/HHH-551?page=worklog

Looking through the source to gain an idea of how much is involved here, I can see a fair number of TODO tags, but I couldn't work out which one was relevant to this issue. Perhaps the ones in JoinWalker.


Top
  
 
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.