-->
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: composite-id with key-many-to-one - how to do join?
PostPosted: Wed Aug 02, 2006 8:01 am 
Newbie

Joined: Thu Jul 27, 2006 10:02 am
Posts: 17
I have one-to-many relationship defined in my Set object.

Code:
<bag name="points" inverse="true" lazy="false" cascade="all">
  <key column="SETID"/>           
  <one-to-many class="PointSet" />           
</bag>


and the composite key on the SetPoint object

Code:
<composite-id>
   <key-property name="startDate"/>
   <key-many-to-one name="point" lazy="false" class="Point"/>
   <key-many-to-one name="set" lazy="false" class="Set"/>       
</composite-id>


When I do a search for a Set hibernate generates a query to find a Set and then one query for each point - very inneficient.

When I change my composite key to this

Code:
<composite-id>
  <key-property name="startDate"/>
  <key-property name="pointId"/>       
  <key-many-to-one name="set" lazy="false" class="Set"/>       
</composite-id>


And add many-to-one relationship

Code:
<many-to-one
    name="point"
    column="POINTID"
    class="Point" not-null="true"
    insert="false" update="false"/>


It works as expected. One query to find Set and one to find all the points for the Set, but the problem is when I'm adding new Point to Set and want to persist it because I have pointId and Point objects in my PointSet hibernate is saying that this two are different.

I think first approach is how it should be implemented, but is there a way to force a join so it does only a single select for all the points rather than one for each point.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 1:57 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Try this

Code:
<bag name="points" inverse="true" lazy="false" cascade="all" fetch="join">
  <key column="SETID"/>           
  <one-to-many class="PointSet" />           
</bag>


This should force an outer join fetch if you want this as your default behaviour.

Otherwise, depending on how you can actually fetching your data, HQL or CriteriaAPI you can tell it how you would want that collection fetched.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 2:04 pm 
Newbie

Joined: Thu Jul 27, 2006 10:02 am
Posts: 17
The problem is with many-to-one relationship not one-to-many so I would have to add fetch="join" inside my composite key:

Code:
<key-many-to-one name="point" lazy="false" class="Point"/>


which as far as I know.. I can't.

Hibernate generates many SQL queries to get a Point for each Point Set, and this is were the problem is.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 2:38 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
You don't need it in your composite key, it's the Set that is driving the fetch of the PointSet bag.


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.