-->
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: sort and limit child collection
PostPosted: Mon Mar 03, 2008 4:15 pm 
Newbie

Joined: Mon Mar 03, 2008 4:07 pm
Posts: 4
Hello,

Is there a way to apply a limit on the child collection mapping?

For example, I have Department and Employees as below
Code:
public class Department {
  private Collection<Employee> topEmployees;

  @OneToMany(mappedBy="department", fetch=FetchType.EAGER)
  public Collection<Employee> getTopEmployees() {
      return topEmployees;
  }
}


What can I do so that I only get top 5 employees from the department?

I looked into using Filter, but couldn't figure out how to limit without using subselect, and MySQL doesn't support limit on subselects.

Thanks!
jydev


Top
 Profile  
 
 Post subject: Re: sort and limit child collection
PostPosted: Mon Mar 03, 2008 4:25 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
If you can't express the condition with @Where then chances are custom loaders are your last chance.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 9:30 pm 
Newbie

Joined: Mon Mar 03, 2008 4:07 pm
Posts: 4
Farzad,
Thanks for your reply. I am trying to use the custom loader, but having issues.

I am getting "Named SQL query not known" error. When I stepped through the Hibernate source, for some reason it is calling getNamedSQLQuery(), rather than getNamedQuery(). I tried to track down how to trigger it to call getNamedQuery(), but the source path branches way before these calls are made and was a difficult to track down.

Here is my annotated sample:

Code:
public class Department {
  private Collection<Employee> topEmployees;

  @OneToMany(mappedBy="department", fetch=FetchType.EAGER)
  @Loader(namedQuery="topEmployees")
  public Collection<Employee> getTopEmployees() {
      return topEmployees;
  }
}

@NamedQuery(name="topEmployees",
        query="select blah blah..." )
//@NamedNativeQuery(...)
public class Employee {
}


What am I doing wrong? Note that if I swith over to NamedNativeQuery, it works.

Using Hibernate 3.2.5 and HibernateAnnotations 3.3.0
Thanks,
jydev


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 9:35 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
That's actually a valid point but as far as I know custom loaders work with native queries so you won't have a choice here.



Farzad-


ps. I might be totally wrong so let me know if you find a way around it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 10:21 pm 
Newbie

Joined: Mon Mar 03, 2008 4:07 pm
Posts: 4
Thanks for the quick reply.

I lied about everything working ok with native query. I don't get hibernate exceptions, however my collection (topEmployees) is null.

I traced the code and when Hibernate retrieves the results set, there are results. But eventually, the call ends up at the following, which doesn't return anything (return type of void).
Code:
public void initialize(Serializable key, SessionImplementor session)
throws HibernateException {
                ...
      query.setCollectionKey( key )
            .setFlushMode( FlushMode.MANUAL )
            .list();
}


So where and how is the results from the query being mapped to the parent (in this example Department).

Thanks again,
jydev


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 10:29 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
That means hibernate does not know it is a collection loader. In the xml mapping way there is a tag that specifies this. I don't know what the equivalent annotation is, but I will look and tell you if I find anything.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 11:01 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Take a look at this one. it might help.

http://forum.hibernate.org/viewtopic.php?t=982986



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 05, 2008 2:44 pm 
Newbie

Joined: Mon Mar 03, 2008 4:07 pm
Posts: 4
Thanks. I finally got it working using sql-query in mapping file.
It's weird. The documentation seems to indicated it supports @NamedQuery (hql), but the code always looks calls getNamedSQLQuery(). I also tried defining hql named query (using query tag instead of sql-query) in the mapping file and the same thing happens... Oh well. I think my query was more efficient using SQL, so it worked out. However, it would have been nice to define this using annotation. I couldn't find annotation equivalent for load-collection.

Anyway, thanks a lot for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 05, 2008 2:46 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
jydev wrote:
I couldn't find annotation equivalent for load-collection.



There doesn't seem to be any. If I recall correctly everywhere I looked it is said to just put the @Loader and it flies. However, in my case I get NPEs due to uninitialized collections. I would like to look at this to find out what's wrong but I might not be able to do it very soon. In any events, it's good you solved your problem.


Farzad-


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.