-->
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.  [ 7 posts ] 
Author Message
 Post subject: one-to-one where foreign-key is not foreign table's primary
PostPosted: Wed Jul 11, 2007 5:47 pm 
Newbie

Joined: Thu Jun 21, 2007 11:05 am
Posts: 5
Hi there, I have what I think should be a simple problem, which is causing me to bang my head against the wall.

The scenario is this, I have two tables:

Code:
TABLE_A
======
ID
NAME
DESC
REF_LOAN_NUM

TABLE_B
======
ID
FNAME
LNAME
LOAN_NUM



Now, what I would like to have is one object:
Code:
public class A {
   Integer id;
   String name;
   String description;
   String clientFirstName;
   String clientLastName;

//all getters/setters here//
}

I would like a Mapping document for A such that there is a join between TABLE_A and TABLE_B where TABLE_A.REF_LOAN_NUM = TABLE_B.LOAN_NUM. I know this database schema is a$$, but I didn't design it! I just have to live with it.

Anyone have any clue how I might do this? I thought I could get away with adding this entry to the mapping document:

Code:
<join table="TABLE_B">
   <key column="REF_LOAN_NUM" property-ref="LOAN_NUM"/>

      <property name="clientLastName"
         column="LNAME"
         type="string"/>

      <property name="clientFirstName"
         column="FNAME"
         type="string"/>

</join>



However I soon realized that "property-ref" doesn't accept a column name.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 11, 2007 7:22 pm 
Beginner
Beginner

Joined: Mon Jun 25, 2007 11:57 pm
Posts: 28
Might be worth a look:

http://forum.hibernate.org/viewtopic.php?t=976874


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 11, 2007 8:05 pm 
Beginner
Beginner

Joined: Mon Jun 25, 2007 11:57 pm
Posts: 28
Also, if that doesn't help you, look at page 98 of the hibernate_reference pdf :

7.6. More complex association mappings

This allows you to define an arbitrary property element and then use that as the property-ref


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 11, 2007 8:43 pm 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
Hi if you get an idea how to resolve this kind of issue please let me know thank you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 1:42 pm 
Newbie

Joined: Thu Jun 21, 2007 11:05 am
Posts: 5
Nope, I just can't seem to figure this out! I think part of the problem is, sometimes with hibernate mappings it's tough to figure out the context of some of the mapping elements.

For instance, why would I not be able to specify a *column* name for a reference in the foreign table? Like:

<key column="REF_LOAN_NUM" foreign-ref="LOAN_NUM"/>

I don't know, I'm really finding Hibernate good for simple mappings, and confusing as he11 for anything else. But, obviously others are able to figure this type of thing out, so it is probably just me.

All I want to do is have a class which gets some of it's properties from another table via a join. I can't seem to do that when the join column in TABLE_B is not it's primary key. In theory, the join column should be it's primary key, in reality people creating and adapting schema's "cheat".

If anyone else has any ideas please let me know


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 5:40 pm 
Newbie

Joined: Thu Jun 21, 2007 11:05 am
Posts: 5
I managed to solve my problem using an <sql-query> tag defining a full SQL statement with join (iBatis-type approach). I guess the other way would be to use a view.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 2:54 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
Hi craiger316,
You are right. As I searched over the internet, don't know why Hibernate doesn't provide easy way to mapping for these type of many-to-many association.


As you said, I hope you can below peice of exapmle..

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
class="Address"/>
</set>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>

create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )


Only thing you have to create a view to create join table like PersonAddress.

Regards
Shanmugam.N


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