-->
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.  [ 7 posts ] 
Author Message
 Post subject: Criteria with restriction on associated object's property
PostPosted: Wed Aug 23, 2006 5:49 am 
Newbie

Joined: Wed Aug 23, 2006 5:17 am
Posts: 4
Hi.
I'm trying to execute a Criteria query which filters the root entity using fields contained in it's many-to-one associated objects. It should be easy to do but, I got strange errors.

My database looks like this:
Student table has FK that references Group table
Group Table has FK that references Teacher table.
Theese two associations are mapped as many-to-one in hibernate.

Now I try Criteria query like this:

Criteria crit = session.createCriteria(Student.class)
Criteria crit2 = crit.createCriteria("group") // 'group' is a property in Student class.
crit2.add(Expression.eq("teacher",t)); //'teacher' is a property in Group class, t is a Teacher object.

I got this exception:

org.hibernate.exception.SQLGrammarException: could not execute query
...
Caused by: java.sql.SQLException: Unknown column 'group1_.teacher_id' in 'where clause'

When I looked at the SQL query generated by hibernate it turned out, that the Group table wasn't in FROM clouse at all, but in WHERE clause was 'group1_.teacher_id=?'.

The above works well for one-to-many associations (ex. when I query Group class and make some conditions on fields in Student). How should it look for many-to-one? Is it posiible that the fact I use composite PK is messing up?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 5:29 am 
Newbie

Joined: Wed Aug 23, 2006 5:17 am
Posts: 4
Oh, I forgot to add last statement. IT should be:

Criteria crit = session.createCriteria(Student.class)
Criteria crit2 = crit.createCriteria("group") // 'group' is a property in Student class.
crit2.add(Expression.eq("teacher",t)); //'teacher' is a property in Group class, t is a Teacher object.

List list = crit.list();

The last statement couses the exception.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 5:53 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
try with:
Code:
Criteria crit = session.createCriteria(Student.class)
   .add(
      Expression.eq("group.teacher", t)
   );

List list = crit.list();


Please, don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 6:09 am 
Newbie

Joined: Wed Aug 23, 2006 5:17 am
Posts: 4
Thanks, but I tried that. I got this exception:

org.hibernate.QueryException: could not resolve property: group.teacher of: pl.teb.entities.Student

And no SQL query is generated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 6:57 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Then try with:
Code:
Criteria crit = session.createCriteria(Student.class)
   .createAlias("group.teacher", "teacher")
   .add(
      Expression.eq("teacher", t)
   );

List list = crit.list();


Also, I think that in your first code you should have:
Code:
List list = crit2.list();


Please, don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 8:54 am 
Newbie

Joined: Wed Aug 23, 2006 5:17 am
Posts: 4
I tried it and I got the exception:

org.hibernate.QueryException: could not resolve property: teacher of: Student

On list() operation. Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 10:49 am 
Newbie

Joined: Fri Aug 25, 2006 10:18 am
Posts: 8
Location: England
It looks like you are trying to query by Example, so I would suggest the following:

Code:
    Criteria  crit = session.createCriteria(Student.class)
    Criteria crit2 = crit.createCriteria("group");
    crit2.add(Example.create(t)); 
    List list = crit.list();


Please, don't forget to rate.


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