-->
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: Inverse one-to-one mapping?
PostPosted: Fri Apr 27, 2007 12:46 am 
Newbie

Joined: Sat Jan 22, 2005 3:10 pm
Posts: 6
Hi,

I'm trying to do a unidirectional one-to-one mapping for a legacy database where the primary key of the other side of the join is available on the owning side, but not the other way around...

Code:
class A {

  // PK
  String getNewGUID() ...
 
  // Regular property known to be unique and one-to-one with B.getId()
  String getOldId() ...

  // How to specify this as a one-to-one?
  B getB() ...

  ...
}

class B {

  // PK
  String getId() {...}

  ...
}


From the docs I get the impression this could be done with a formula:

"Almost all one to one associations map to the primary key of the owning entity. In the rare case that this is not the case, you may specify a some other column, columns or expression to join on using an SQL formula."

But I'm not clear on what the syntax would be. I've taken a look at the org.hibernate.test.onetoone.formula example, but that is a slightly different scenario.

Also, this mapping is for read-only data.

Any pointers would be most appreciated.

Thanks,

Ian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 2:04 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
I have set up a small example. It deals with unidirectional one-one associations from both sides, separately.
( with and without formula )

Tables:
X:
id number
name varchar2
Y:
id number
name varchar2
ref_id number ( refers to X's id )


1) One to One , unidirectional from X to Y.
<class name="X" table="x">
<id name="id" column="id" type="integer" />
<property name="name" type="string" column="name"/>
<one-to-one class="Y" name="y" property-ref="ref_id" />
</class>

<class name="cars.models.Y" table="y">
<id name="id" column="id" type="integer" />
<property name="name" type="string" column="name"/>
<property name="ref_id" column="ref_id"/>
</class>


2) One to One , unidirectional from Y to X.

i) one way.
<class name="cars.models.X" table="x">
<id name="id" column="id" type="integer" />
<property name="name" type="string" column="name"/>
</class>

<class name="Y" table="y">
<id name="id" column="id" type="integer"/>
<property name="name" type="string" column="name"/>
<one-to-one class="X" name="x" >
<formula>ref_id</formula>
</one-to-one>
</class>


ii) another way. ( X remains the same )
<class name="Y" table="y">
<id name="id" column="id" type="integer"/>
<property name="name" type="string" column="name"/>
<many-to-one class="X" name="x" column="ref_id" />
</class>


-------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 11:47 am 
Newbie

Joined: Sat Jan 22, 2005 3:10 pm
Posts: 6
Quote:
Code:
<class name="Y" table="y">
<id name="id" column="id" type="integer"/>
<property name="name" type="string" column="name"/>
<one-to-one class="X" name="x" >
<formula>ref_id</formula>
</one-to-one>
</class>


Thanks, that worked perfectly. Though I wish I understood formula a little better. So "ref_id" gets translated to "[y].ref_id=[x].[id]" or "[x].[id]=?" (where ? is the previously selected value of [y].ref_id) for joins/subselects or extra selects respectively?

Not to complain, as I'm getting a lot for free here, but is this explained somewhere outside of the source that I'm missing? If not I'd like to take a stab a contributing some documentation.

Thanks Again,

Ian


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 1:53 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi Ian,
Was out of desk for some time.
>>> So "ref_id" gets translated to "[y].ref_id=[x].[id]" or "[x].[id]=?" (where ? is the previously selected value of [y].ref_id) for joins/subselects or extra selects respectively?
I don't get you completely ? Can you put more light into it ?
For the mapping shown above, we get [y].ref_id=[x].[id] in the query.


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.