-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria+Set(Many-to-Many)
PostPosted: Thu Sep 07, 2006 1:32 pm 
Newbie

Joined: Sat Jan 21, 2006 8:30 pm
Posts: 6
Hi,

I'm using a Criteria to get a Bean with its ID and try to fetch some of its relationships dynamically.
I checked the forum and couple other sites but I couldn't find something similar. Sorry if it has been discussed before.


The code is:

Code:
Criteria oCriteria = mSession.createCriteria(Vehicle.class);
oCriteria.add(Expression.idEq(123));
oCriteria.SetFetch("parts", FetchMode.JOIN);
List<Vehicle> oResults = (List<Vehicle>) oCriteria.list();


I'm expecting one result but I actually get the Vehicle + the parts mapped to it.
So if this Vehicle has 2 parts I'm getting 3 results like:
VehiclePart#345
Vehicle#123
VehiclePart#567

I'm using Criteria to be able to use the Fetch functionnality (I'm trying to avoid the Hibernate.initialize() or the OpenSessionInView pattern).

Code:
<class name="Vehicle" table="vehicles">
   <id name="vehicleID" unsaved-value="0">
      <generator class="native" />
   </id>
   <property name="creationTime" not-null="true"/>
   <set name="parts" table="VehicleToParts" lazy="true">
      <key column="vehicleID" not-null="false"/>
      <many-to-many column="partID" class="VehiclePart"/>
   </set>
</class>


Am I doing it the wrong way?
Is there a way to get only the bean I'm querying (i.e. the class specified in the Criteria)?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 7:26 pm 
Newbie

Joined: Thu Sep 07, 2006 7:23 pm
Posts: 2
Hi, Did you ever figure this out. I am running into something similar. I have used this technique on another project and it worked okay. The only difference I can see is that in the other project (where it worked) my m2m relationships were bi-directional. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 9:40 am 
Newbie

Joined: Sat Jan 21, 2006 8:30 pm
Posts: 6
bozmanxx wrote:
Hi, Did you ever figure this out. I am running into something similar. I have used this technique on another project and it worked okay. The only difference I can see is that in the other project (where it worked) my m2m relationships were bi-directional. Thanks.


Not yet.
I can't make it bi-directional here but that's good to know.

I tried with a FetchMode.SELECT (I should expect a second SELECT query right? I even set fetch="subselect"), the "parts" are still uninitialized after list().


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 11:34 am 
Newbie

Joined: Thu Sep 07, 2006 7:23 pm
Posts: 2
I had tried exactly the same things: fetch="subselect"/FetchMode.SELECT to no avail. I think FetchMode.SELECT is the new name for LAZY, i.e., it does additional selects later if needed. We close our Hib session quickly and must always get everything we will need before closing it to avoid a LazyInitEx.

I found this forum topic: http://forums.hibernate.org/viewtopic.p ... af59ac91d9

And when I added a call to c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
that fixed my problem! I am no longer getting more rows than expected and now I can explicitly control whether children are loaded or not. Somehow I was under the impression that making the root distinct was something that Hibernate did automatically. I need to go back and double-check my other project where it seems to be working fine without making that call.

In my case children of the root are not loaded by default (lazy="true") and when I want them I apply setFetchMode("child-name". FetchMode.JOIN) and setResultTransformer(...) in which case the child records are efficiently loaded with a single trip to the DB. Hope this helps...


Top
 Profile  
 
 Post subject: thanks much
PostPosted: Mon Nov 20, 2006 3:40 pm 
Newbie

Joined: Wed Feb 08, 2006 10:53 am
Posts: 3
Thanks much bozmanxx!

it(adding setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);) worked.

cheers


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