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