-->
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: Joining two unrealted tables
PostPosted: Tue Jan 31, 2006 10:31 am 
Beginner
Beginner

Joined: Sun Jan 22, 2006 8:47 pm
Posts: 31
Hi,
I need help...I have a problem like I have 2 unrelated tables...
I associated them using <one -to many>..The main issue is I need to join them on Keys which r not primary or foreign keys...
The tables r not related and there r no common keys..

I specified in hbm file:
<many-to-one property-ref="mykey"/>

Though I specified the key on which to do join using
" property-ref", Hibernate is joining on primary keys of the associated class..How to tell hibernate to do join on my specific keys..
There r no fields commonn to both tables...
Any idea how to do this wuld be grt help..
Thnx,.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 2:11 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 6:56 am
Posts: 29
Do you really need to have this as an association? You can do the JOIN in the query itself... e.g.:

Code:
FROM
     A a,
     B b

WHERE a.foo = b.bar


I don't know if it's possible to make it a proper association (because entities are identified by their ID -> the primary key). But perhaps using a formula/filter on the collection could work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 2:22 pm 
Newbie

Joined: Tue Sep 02, 2003 9:17 am
Posts: 9
Location: London
assaf27 wrote:
Do you really need to have this as an association? You can do the JOIN in the query itself... e.g.:

Code:
FROM
     A a,
     B b

WHERE a.foo = b.bar


I don't know if it's possible to make it a proper association (because entities are identified by their ID -> the primary key). But perhaps using a formula/filter on the collection could work.


I guess you could make it work as a join...I'm also having this issue, and am a bit confused as to why it doesn't work. My mapping looks something like the following:

Code:
<class name="User" table="USER">
      <id name="id" column="USER_ID" unsaved-value="null">
         <generator class="native" />
      </id>
      <property name="login" column="LOGIN" type="string" length="48"/>
      <property name="objectId" column="OBJECT_ID" type="integer" />
      
      <join table="V_USER" inverse="true" fetch="join">
         <key column="OBJECT_ID"
            property-ref="objectId"
            update="false" />
            <property name="ldapId" column="LDAP" type="string" update="false" insert="false" />
      </join>
      
   </class>


Even though the property-ref is set, it always joins on the User entity's primary key, when what it should be doing (if property-ref does what it says on the tin) is joining on the OBJECT_ID column instead.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 2:24 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 8:47 pm
Posts: 31
Hi,
Thnx for the reply..Ya this kind of join works..

But the condition tht I have specified is in subselect of other query..I have a condition where in the main query iam using a primary key of A for join and in subquery I am using other key of A to join with a different table...

So I have to specify the keys to join on....

Any idea how to do?By the way how do v use filters
Thnx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 2:25 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Can you show your mapping files and the query ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 3:17 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 8:47 pm
Posts: 31
<hibernate-mapping>

<class name="Appt" table="appt">

<id name="apptId" type="int" column="appt_id" >
<generator class="assigned"/>
</id>

<property name="physId" column="phys_id" type="int" />

<set name="resInfo" lazy="false" table="resInfo" inverse="true">
<key column="res_id"/>
<one-to-many class="ResInfo" />
</set>


</class>

</hibernate-mapping>
_________________________________________
<hibernate-mapping >

<class name="resInfo" table="resInfo">

<id name="resId" column="res_id" >
<generator class="assigned"/>
</id>
< property name="lastName" column="last_name" type="string" />

<many-to-one name="appt" class="Appt" insert="false"
update="false" property-ref="physId" fetch="select" unique="true" /> -->1***
</class>
</hibernate-mapping>

___________________________________________
String SQL_QUERY="select appt.apptId,(select resInfoAppt.lastName from appt.resInfo resInfoAppt ) from ResUnit resunit inner join resunit.apptSet appt ";
_____________________________

SelQuery using Hibernate Query Language
Hibernate:

select
apptset1_.appt_id as col_0_0_,
(select
resInfo2_.last_name
from
resInfo resInfo2_
where
apptset1_.appt_id=resInfo2_.res_id) as col_1_0_ --->2***
from
resunit resunit0_
inner join
appt apptset1_
on resunit0_.appt_id=apptset1_.appt_id

My problem is I have specified in line -->1 above

<property-ref="physId"> which is not primary key..

but in the hibernate's select clause on line -->2 its implicitly joining on
appt's primary key which is appt_id..

Why doesnt hibernate do join like in line -->2

apptset1_.phys_id=resInfo2_.res_id

instead of

apptset1_.appt_id=resInfo2_.res_id

Thnx for your help..pls help me what to do?

_________________________________________________


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 3:28 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Code:
<set name="resInfo" lazy="false" table="resInfo" inverse="true">
<key column="res_id"/>
<one-to-many class="ResInfo" />
</set>

should be
Code:
<set name="resInfo" lazy="false" table="resInfo" inverse="true">
<key column="res_id" property-ref="physId" />
<one-to-many class="ResInfo" />
</set>


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.