File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate: Different Join in Save() and Get() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: Different Join in Save() and Get()" Watch "Hibernate: Different Join in Save() and Get()" New topic
Author

Hibernate: Different Join in Save() and Get()

Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
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 :



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

Actually i want different joins for save() and get() respectively..... This is possible in JDBC but how i can achieve samething in
Hibernate ?
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
There seems to be a little problem.

I am wondering how can you have one to one relationship between the two tables, if the PK of one table is mapped to the "part" of the PK of the other table, as in your case.
Doesn't it became a many-to-one relationship?


Rahul Babbar
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
Rahul Actually there are 2 relationship between Table1 and Table2 <one-to-one> and <one-to-many>
It is the same case as described in "Java Persistence With Hibernate book" : Item and Bid classes.

class Item{
Bid successful;
List bids;
}

The problem is not of association, the main query is
" i want different joins for save() and get() respectively..... This is possible in JDBC but how i can achieve samething in Hibernate ?"
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
I am not sure how you can get around this problem.

You may want to create a view from the two tables, and make its definition in such a way that the view returns whatever you are trying to select.
Save() operation on the table should remain the same...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: Different Join in Save() and Get()
 
Similar Threads
One-to-one:property-ref to a part of composite-key
Critial Problem with one-to-one association.
Mapping Associations using composite-key element ,property-ref attribute and <formula> element
TransientObjectException while fetching data.
Hibernate problem - creating duplicate foreign key index