-->
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.  [ 8 posts ] 
Author Message
 Post subject: conditions on a set one-to-may, the condition can't work...
PostPosted: Tue Sep 06, 2005 4:18 am 
Newbie

Joined: Wed Jul 27, 2005 11:13 pm
Posts: 10
There is a teacher to teach students, and student is classify by grade(it is a condition):

java:
Code:
         resultList = session.createQuery("select t " +
                " from Teacher t left join t.studentSet s" +
                " where s.grade = 30" +
                " and t.teacherId = 1234 ").list();


XML:
Code:
  ...
  <set name="studentSet" table="student" cascade="all" inverse="false" lazy="false">
                <key column="teacherId" />
                <one-to-many class="Student"
                    />
            </set>
...



when the hql runing, the two sql clauses are occured.
and I get the whole set of sudents in teacher without relating the grade condition("s.grade = 30")
Why? realy doubt to me.....



Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 6:18 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Hi,

Did you look at the SQL statement Hibernate generates? Did you issue this statement directly against your database?

This could give some precious hint about the origin of the problem. If not, post the statement...

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 6:50 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
The query you are running is doing exactly what is written. Namely, returning all teachers who have students whose grade is 30 and whose teacher ID is 1234. The collection of students is not restricted in any way by the query.

You may want to have a look at using collection filters, described here: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-filtering


Top
 Profile  
 
 Post subject: Re: conditions on a set one-to-may, the condition can't work
PostPosted: Tue Sep 06, 2005 11:01 am 
Newbie

Joined: Fri Sep 02, 2005 12:47 pm
Posts: 14
Location: muenchen
william1975 wrote:
There is a teacher to teach students, and student is classify by grade(it is a condition):

java:
Code:
         resultList = session.createQuery("select t " +
                " from Teacher t left join t.studentSet s" +
                " where s.grade = 30" +
                " and t.teacherId = 1234 ").list();


XML:
Code:
  ...
  <set name="studentSet" table="student" cascade="all" inverse="false" lazy="false">
                <key column="teacherId" />
                <one-to-many class="Student"
                    />
            </set>
...



when the hql runing, the two sql clauses are occured.
and I get the whole set of sudents in teacher without relating the grade condition("s.grade = 30")
Why? realy doubt to me.....




Hi

You should try smth like this:

it is sql statement you should find out what is hql for ON

select t from Teacher t left join t.studentSet s ON (t.teacherId = 1234) where s.grade = 30

Sincerely,

Pawel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 11:05 am 
Newbie

Joined: Fri Sep 02, 2005 12:47 pm
Posts: 14
Location: muenchen
I belive that starting from hibernate 3 it should be WITH as a substitue of SQL "ON"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 9:44 pm 
Newbie

Joined: Wed Jul 27, 2005 11:13 pm
Posts: 10
sdknott wrote:
The query you are running is doing exactly what is written. Namely, returning all teachers who have students whose grade is 30 and whose teacher ID is 1234. The collection of students is not restricted in any way by the query.

You may want to have a look at using collection filters, described here: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-filtering


sdknott,
you are exactly right, the HQL did itself things.
But I don't get enough info from the reference doc, do you know how to restricte in the set collection? The actually clause is expected for the example.

regards,
William


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 10:39 pm 
Newbie

Joined: Wed Jul 27, 2005 11:13 pm
Posts: 10
HI,
Reference the sdknott's advice, I got a working way:

Java

Code:
         Teacher t = (Teacher)session.createQuery("select t " +
                " from Teacher t" +
                " and t.teacherId = 1234 ").uniqueResult();
         Collection studentColl = session.createFilter(t.getStudentSet(),
                    " where this.grade = 30")
                    .list();
         t.setStudentSet(new HashSet(StudentColl));


and in hbm.xml of teacher file: should set the property "lazy" as false in StudentSet section, to avoid querying the students set two times.

But the way look seemly complex,
Is there any mother to make it more simple? example use one query clause?

thanks,
William


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 10:49 pm 
Newbie

Joined: Wed Jul 27, 2005 11:13 pm
Posts: 10
Sorry for wrong words and sentences:
I mean:
The way listed looks like very complex,
Is there any method to make it more simple? example use one query clause?



william1975 wrote:
HI,
Reference the sdknott's advice, I got a working way:

Java

Code:
         Teacher t = (Teacher)session.createQuery("select t " +
                " from Teacher t" +
                " and t.teacherId = 1234 ").uniqueResult();
         Collection studentColl = session.createFilter(t.getStudentSet(),
                    " where this.grade = 30")
                    .list();
         t.setStudentSet(new HashSet(StudentColl));


and in hbm.xml of teacher file: should set the property "lazy" as false in StudentSet section, to avoid querying the students set two times.

But the way look seemly complex,
Is there any mother to make it more simple? example use one query clause?

thanks,
William


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