Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.5.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.1.ga</version>
</dependency>
Mapping documents:
JPA
Code between sessionFactory.openSession() and session.close():
Spring managed
Full stack trace of any exception that occurs:
NA
Name and version of the database you are using:
mySQL 5.05
The generated SQL (show_sql=true):
Hibernate: select this_.id as id3_3_, this_.version as version3_3_, this_.address as address3_3_, this_.alias as alias3_3_, this_.birthDate as birthDate3_3_, this_.browser as browser3_3_, this_.defaultExerciseType as defaultE7_3_3_, this_.email as email3_3_, this_.joined as joined3_3_, this_.logins as logins3_3_, this_.name as name3_3_, this_.password as password3_3_, this_.payingUserUntilDate as payingU13_3_3_, this_.picture as picture3_3_, this_.sexType as sexType3_3_, this_.userType as userType3_3_, this_.validated as validated3_3_, trainingsc4_.PERSON_id as PERSON1_, trainingsc1_.id as training2_, trainingsc1_.id as id6_0_, trainingsc1_1_.version as version6_0_, trainingsc1_1_.PERSON_ID as PERSON3_6_0_, trainingsc1_.date as date7_0_, trainingsc1_.number as number7_0_, person6_.id as id3_1_, person6_.version as version3_1_, person6_.address as address3_1_, person6_.alias as alias3_1_, person6_.birthDate as birthDate3_1_, person6_.browser as browser3_1_, person6_.defaultExerciseType as defaultE7_3_1_, person6_.email as email3_1_, person6_.joined as joined3_1_, person6_.logins as logins3_1_, person6_.name as name3_1_, person6_.password as password3_1_, person6_.payingUserUntilDate as payingU13_3_1_, person6_.picture as picture3_1_, person6_.sexType as sexType3_1_, person6_.userType as userType3_1_, person6_.validated as validated3_1_, trainingse2_.id as id9_2_, trainingse2_.version as version9_2_, trainingse2_.exercise_id as exercise3_9_2_, trainingse2_.trainingScheme_id as training4_9_2_ from PERSON this_ inner join PERSON_TrainingScheme trainingsc4_ on this_.id=trainingsc4_.PERSON_id inner join TrainingScheme trainingsc1_ on trainingsc4_.trainingSchemes_id=trainingsc1_.id left outer join TrainingSchemeAbstract trainingsc1_1_ on trainingsc1_.id=trainingsc1_1_.id left outer join PERSON person6_ on trainingsc1_1_.PERSON_ID=person6_.id inner join TrainingSet trainingse2_ on trainingsc1_.id=trainingse2_.trainingScheme_id where trainingse2_.exercise_id in (?, ?) order by this_.birthDate desc limit ?
Debug level Hibernate log excerpt:
So this is what I am doing :
public List<Person> searchForSchemes(int first, int count, String property,
boolean ascending, SearchParameters searchParameters) {
Criteria criteria = getHibernateSession().createCriteria(Person.class);
criteria.setMaxResults(count);
criteria.setFirstResult(first);
if (ascending) {
criteria.addOrder(Order.asc(property));
} else {
criteria.addOrder(Order.desc(property));
}
Person person = searchParameters.getPerson();
if (person != null && person.getSexType() != null) {
criteria.add(Restrictions.eq("sexType", person.getSexType()));
}
if (person != null && person.getBirthDate() != null) {
DateTime time = new DateTime(person.getBirthDate());
Date timeAfter = time.minusYears(searchParameters.getOffset())
.toDate();
log.debug("timeAfter" + timeAfter.toLocaleString());
Date timeBefore = time.plusYears(searchParameters.getOffset())
.toDate();
log.debug("timebefore " + timeBefore.toLocaleString());
criteria.add(Restrictions.between("birthDate", timeAfter,
timeBefore));
}
if (searchParameters.getExercises().size() > 0) {
criteria=criteria.createCriteria("trainingSchemes").createCriteria("trainingSets").add(
Restrictions.in("exercise", searchParameters
.getExercises()));
}
return (List<Person>) criteria.list();
}
So this criteria, were supposed to return 2 persons, but instead returns one person per trainingscheme (thats 18), although I only have 3 unique persons in all. It's only the part with bold that does not work. It's like it erases rest of the criteria.
What am I doing wrong?