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: Hibernate: Different Join in Save() and Get()
PostPosted: Mon Mar 30, 2009 9:54 am 
Beginner
Beginner

Joined: Wed Aug 22, 2007 5:53 am
Posts: 38
I have 2 tables viz. TABLE1 and TABLE2:

TABLE1 has it's PK as {ID}
TABLE2 has composite-id as {A,B,C}... TABLE2 also has a column {D}

TABLE1 and TABLE2 are associated with one-to-one mapping TABLE1.ID=TABLE2.A
Cascade-save is on.

Case1:Save()
I want TABLE1.ID=TABLE2.A and TABLE2.B='1' and TABLE2.C='2'

Case2:Get()
I want TABLE1.ID=TABLE2.A and TABLE2.B='1'and TABLE2.D='4'

How should i define this mapping?
Presently i have done like this :

Code:
  <class name="Table1" table="TABLE1"  optimistic-lock="version">
    <id name="Id" column="ID">
      <generator class="assigned" />
    </id>
   
    <one-to-one name="table2" class="Table2" cascade="save-update">
   <formula>'1'</formula>
     <formula>'2'</formula>
     <formula>'3'</formula>
    </one-to-one>
   </class>
     
  <class name="Table2" table="TABLE2" optimistic-lock="version">
     
    <composite-id >
   <key-property name="a" column="A" />
   <key-property name="b" column="B" />
   <key-property name="c" column="C" />
    </composite-id>

    <property name="d" column="D" />
    <many-to-one name="table1" class="Table1" column="A" insert="false" update="false"/>
  </class> 


Save is working fine... but when it comes to Get(), the sql query is:

select *
from TABLE1 t1 left outer join TABLE2 t2 on '1'= t2.A and '2'=t2.B and '3'=t2.C where t1.ID=?

which is incorrect...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 30, 2009 2:22 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The three <formula> tags are matched with the three <key-property> tags in the order they are defined. So you would need:

Code:
<formula>ID</formula>
<formula>'1'</formula>
<formul>...</formula>


First formula matches with first key-property to create TABLE1.ID=TABLE2.A. The second formula matches with the second key-property to create '1'=TABLE2.B. The third formula will be matched with the third key-property (TABLE2.C). You can't match this to TABLE2.D.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 1:49 am 
Beginner
Beginner

Joined: Wed Aug 22, 2007 5:53 am
Posts: 38
Yes... That mapping was typo error... The correct mapping is :
Code:
<class name="Table1" table="TABLE1"  optimistic-lock="version">
    <id name="Id" column="ID">
      <generator class="assigned" />
    </id>
   
    <one-to-one name="table2" class="Table2" cascade="save-update">
   <formula>ID</formula>
     <formula>'1'</formula>
     <formula>'2'</formula>
    </one-to-one>
   </class>
     
  <class name="Table2" table="TABLE2" optimistic-lock="version">
     
    <composite-id >
   <key-property name="a" column="A" />
   <key-property name="b" column="B" />
   <key-property name="c" column="C" />
    </composite-id>

    <property name="d" column="D" />
    <many-to-one name="table1" class="Table1" column="A" insert="false" update="false"/>
  </class> 


but my main question is :
Quote:
Actually i want different joins for save() and get() respectively..... This is possible in JDBC but how i can achieve samething in
Hibernate ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 2:12 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Actually i want different joins for save() and get() respectively


What do you mean with 'join for save'? A save typically results in an insert and that can't have any joins.


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.