-->
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.  [ 2 posts ] 
Author Message
 Post subject: Additional constraint on collection mapping
PostPosted: Fri Mar 14, 2008 5:35 pm 
Newbie

Joined: Fri Mar 14, 2008 4:56 pm
Posts: 1
Location: Canada
Hi,

My task is to provide Hibernate-based access layer to a legacy database and no changes are allowed to the schema. This creates few challenges in describing Hibernate mapping. Being a beginner in Hibernate I am looking for a help from this forum.

I need to describe one-to-many relationship between parent table PARENT and child table CHILD. In the Java layer it is modelled as collection of Child elements in Parent class. Child table has foreign key FK_PARENT which links it to a primary key of parent table KEY. But join also has an additional constraint. Both tables have column SEQ_NUM and join must be done with the following condition: PARENT.KEY = CHILD.FK_PARENT and PARENT.SEQ_NUM = CHILD.SEQ_NUM.

Hibernate allows describe collection of children related by foreign key only (at least it’s my understanding of Hibernate documentation and DTD). Looks like the only option for additional constraint would be to use attribute “where”, but I am not sure if Hibernate would allow constraint in the following form: where=” PARENT.SEQ_NUM = CHILD.SEQ_NUM”.

Any help would be appreciated,
Thanks


Top
 Profile  
 
 Post subject: Re: Additional constraint on collection mapping
PostPosted: Fri Mar 14, 2008 6:26 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
There are three things you can do.

The legal way is to do a subselect:

Code:
<set name="children"...>

   <subselect>select c.* from Child c inner join Parent p on c.ParentId = p.Id and c.SEQ_NUM = p.SEQ_NUM</subselect>
.....
</set>



Which is quite performance inefficient. Second option is write a custom loader which is both legal and efficient. The third option is to use hibernate generated aliases in the where property:

Code:
<set ..... where="parent_0.SEQ_NUM = child_0.SEQ_NUM" ...>



if you are wondering what the alias is going to be you need to run hibernate once and see the generated sql. However, this is cheating and since cheating is not a good thing you will be nailed at some point some how. So use this at your own responsibility or make sure you leave the company before they decide to upgrade hibernate jars.


Farzad-


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