-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria with alias's
PostPosted: Sun Mar 30, 2008 8:04 am 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 4:38 am 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
The Alias part should of course look like this instead:

criteria=criteria.createAlias("trainingSchemes", "ts").createAlias(
"ts.trainingSets", "tst").add(
Restrictions.in("tst.exercise", searchParameters
.getExercises()));

But still not working...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 6:24 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
try use this

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 8:13 am 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
ferozz wrote:
try use this

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);


It seems to work.. However my test preperation fails.. So I cant really confirm it yet.

Im talking about the issue here:
http://forum.hibernate.org/viewtopic.php?t=985259


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 11:17 am 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
ferozz wrote:
try use this

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);


can confirm that it works:) Thanks


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