-->
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.  [ 1 post ] 
Author Message
 Post subject: Criteria API - Advanced Queries
PostPosted: Wed Sep 03, 2008 7:37 am 
Newbie

Joined: Wed Sep 03, 2008 7:20 am
Posts: 3
I am trying to use Criteria API to build an Advanced Search form...

I have two Entities related by a bidirectional association

Book has One or Many Records and Record is related with One Book...

The association is correctly mapped in both directions using Lazy Collections (in Book, in order to get all Records related) and in Record, in order to get the related Book.

I try to build a Criteria query which allows to me to get the books whose SOME of their records has a date between two limits given...

I try it doing something like this...


Code:

criteria.createCriteria(Book.class,"book")
          .createAlias("book.records", "records")
     .add(Restrictions.between("records.date", date1, date2))
     .setFetchMode("records", FetchMode.JOIN);





or

Code:

criteria.createCriteria(Book.class)
    .createCriteria("records")
    .add(Restrictions.between("date",date1,date2));




(Two queries retrieved the same result)

where "records" is the attribute in Book which maps a List of Records related. The query works, but each time it found a hit Record, the related Book is stored as result... and finally, If there is only one Book with 100 Records matching, I have 100 Books (the same 100 times) in the result...

I want that if there is one Book with 100 Records, if 1 / 2 / 50 Records are between the limits given, only ONE Book appears as result.

What am I doing wrong?

Thank you in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.