-->
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.  [ 8 posts ] 
Author Message
 Post subject: Fetching only one child
PostPosted: Tue Sep 18, 2007 9:49 am 
Newbie

Joined: Thu May 18, 2006 6:36 am
Posts: 11
Location: Bangalore
Hi,

I have a one-to-many mapping between two of my classes, what I want to archive is that only one of the child gets retrieved when the parent is retrieved.

How can i achieve this, I tried to set lazy="false" and batch-fetch-size as 1, at the same time changed the fetch strategy to Join, but this doesn't seem to be working.

Is there any other way I can achieve this.Can i write a query which can help me do this.I need only one of the child's to be displayed.

Thanks,
Rohit


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 10:34 am 
Newbie

Joined: Wed Sep 12, 2007 1:58 pm
Posts: 12
Take a look at the org.hibernate.Query API, in particular the setFirstResult() and setMaxResults() methods.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 10:41 am 
Newbie

Joined: Thu May 18, 2006 6:36 am
Posts: 11
Location: Bangalore
Hi,

Thanks for the help, but this will return one or more records.

My requirement is that i want all the parent records but only one child for each parent.

Thanks,
Rohit


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 10:58 am 
Newbie

Joined: Thu May 18, 2006 6:36 am
Posts: 11
Location: Bangalore
Hi,

Thanks for the help, but this will return one or more records.

My requirement is that i want all the parent records but only one child for each parent.

Thanks,
Rohit


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 2:22 pm 
Beginner
Beginner

Joined: Fri Sep 08, 2006 7:29 am
Posts: 36
Try this....


1. setFirstResult(1)
2. setMaxResults(1)



Maybe this would work....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 2:31 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Do you need something like this?

Code:
select parent.children from Parent parent where parent.id = ?


and when you execute this set the maximum results to 1.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 3:57 pm 
Newbie

Joined: Mon Sep 17, 2007 1:28 pm
Posts: 6
this is pretty much crap but it would work


public void setChildren(List<Child> children) {
this.children = children.subList(0, 1);
}

obviously you'd want some bounds & null checks in there. More obviously you're probably looking for a solution at the db/hibernate level, just thought I'd throw this out there as a potential idea.

Don't forget to rate


Top
 Profile  
 
 Post subject: Re: Fetching only one child
PostPosted: Thu Dec 01, 2011 6:33 pm 
Newbie

Joined: Tue Apr 12, 2011 2:13 pm
Posts: 1
Is there something new?

I have a Parent with a List of Children. There can be thousands of children which were added over the applications life time.
Often I only want to load the newest child. (I use Criteria, Fluent NHibernate)

Is there meanwhile a meachnismen that (N)Hibernate do not load all children of the list in such a case?
I tried multiple thinks, but have no success.


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