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.  [ 9 posts ] 
Author Message
 Post subject: Mapping a one-to-one relationship with a where clause
PostPosted: Mon May 03, 2004 4:17 pm 
Beginner
Beginner

Joined: Mon May 03, 2004 1:25 pm
Posts: 31
I have a relatioship that in the db (oracle9) is a one-to-many, but in the domain layer needs to be one-to-one. We have counties and populations, a county can have multiple population based on the source, but the application I'm working on, only uses one of the sources.

Here's the my initial mapping:

Code:
<class name="CountySurvey" table="MKT_SEG_SMPL_GEO">
   <jcs-cache usage="read-only"/>
    <id name="id" type="long" column="MKT_SEG_SMPL_GEO_GNMBR">
      <generator class="assigned"/>
      </id>
   <many-to-one name="county" class="County" column="SG_SMPL_GEO_GNMBR"/>      
   <many-to-one name="marketSegment" class="MarketSegment" column="MS_MKT_SEG_GNMBR"/>
   <one-to-one name="population" class="Population" foreign-key="MS_MKT_SEG_GNMBR"/>
</class>
<class name="Population" table="POPULATION" where="CDCH_CMPND_CHRSTC_GNMBR = 42 and PS_POP_SRC_GNMBR = 113">
   <jcs-cache usage="read-only"/>
    <id name="id" type="long" column="POP_GNMBR">
      <generator class="assigned"/>
      </id>
   <property name="value" column="POP_VALUE"/>
</class>


As you can see the Populations class has a where clause with two predicates, that creates the one-to-one situation. When I run a list() on the population class by itself, the class level where clause is part of the query, but when the populations are retrieved as part of the relationship, the where clause is ignored.

Thanks for any help.


Top
 Profile  
 
 Post subject: Forgot the version
PostPosted: Mon May 03, 2004 4:20 pm 
Beginner
Beginner

Joined: Mon May 03, 2004 1:25 pm
Posts: 31
I'm using Hibernate V2.1


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 03, 2004 5:33 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
yep it's normal


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 7:16 am 
Beginner
Beginner

Joined: Mon May 03, 2004 1:25 pm
Posts: 31
Is there anyway to setup the behavior, attach a where clause to a join?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 7:39 am 
Beginner
Beginner

Joined: Sat May 01, 2004 2:44 am
Posts: 32
Location: Brisbane, Australia
duggant wrote:
Is there anyway to setup the behavior, attach a where clause to a join?


You can also set a where clause on any one-to-many relationship, to filter it automatically.

Scott


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 7:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
yes but he will still obtain a list, but with only one element... i might be ok. I also use this strategy in some cases


Top
 Profile  
 
 Post subject: Thanks, using the db to solve it
PostPosted: Tue May 04, 2004 2:01 pm 
Beginner
Beginner

Joined: Mon May 03, 2004 1:25 pm
Posts: 31
Thanks for your help, I created a view to implement the where clause. It has alleviated the problem.


Top
 Profile  
 
 Post subject: Re: Thanks, using the db to solve it
PostPosted: Tue May 04, 2004 7:46 pm 
Beginner
Beginner

Joined: Sat May 01, 2004 2:44 am
Posts: 32
Location: Brisbane, Australia
duggant wrote:
Thanks for your help, I created a view to implement the where clause. It has alleviated the problem.


Ok, if that works for you, then that's good. As far as I know, however, views have limitations on what can be updated and inserted. But if your data is read-only, then that won't be a problem.

What I had meant was that <one-to-many> has a 'where' attribute just like <class> that filters a collection relationship. In your case, you would model your class with a protected or private collection property (for the one-to-many), and a public wrapper property (ie. not mapped using hibernate) that gets/sets the first element only of the private collection property.

Scott


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 9:51 am 
Beginner
Beginner

Joined: Mon May 03, 2004 1:25 pm
Posts: 31
I've recently refactored and solved this problem using the Hibernate3 dynamic filters. It works much better.


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