-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: HQL NEED HELP!!!
PostPosted: Sat Nov 13, 2004 7:34 pm 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Hi All

I have the following objects

House {
String name;
Set persons;
.
.
.
}

and

Person {
String name;
Integer age;
.
.
.
}

each house can have a set of 0 or more persons, but there are some persons that doesn't belong to any house. I need a query to select all houses that contain persons with age<20.

I tried:

from House as h
join h.persons as p
where p.age<20

It doesn't work. What's wrong? Please help!!!!

thanks,
piri


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 7:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
p is a collection, not a person

next time don't add '!!!' in your subject

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 11:03 am 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
Thanks Anthony,

is there a posibility to search thru elements of a Collection?

I saw this in Hibernate documentation:

select account, payment
from Account as account
left outer join account.payments as payment
where :currentUser in elements(account.holder.users)
and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID)
order by account.type.sortOrder, account.accountNumber, payment.dueDate

In this example payment is a collection of payments? I see a property of payment is accessed (payment.currentStatus.name).

Am I missing something?

thanks
piri


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 11:44 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
check in elements() keyword in reference guide

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 8:27 am 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
Hi,

my question is if it's possible to access, in where clause, a property of an item from a collection kind property???

piri


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 7:56 pm 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
Also I tried the following query:

from House as h
where ( select count(*) from h.persons as p where p.age<20 )

and I got some strange kind of objects? What is wrong? Anyone, please help!

thanks
piri


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 8:00 pm 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
Also I tried the following query:

from House as h
where ( select count(*) from h.persons as p where p.age<20 ) > 0

and I got some strange kind of objects? What is wrong? Anyone, please help!

thanks
piri


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 3:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
from House as h
join h.persons as p,
Person person
where person.age<20
and person in elements(p)


in in elements, i said....

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 1:01 pm 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
Hi,

This solutin didn't work, I got an QueryException, then I chenge the query to

select h
from House as h,
Person person
where person.age<20
and person in elements(h.persons)

which makes sense... an I get a few objects of class House$$EnhancerByCGLIB$$56a42fa which contains tha attributes of House class but with null values. very strange! how can I obtain the real House objects with needed condition??? Is this to hard for hibernate?

piri


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 1:14 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Is there something wrong with the documentation?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 1:33 pm 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
Jesus guys, maybe is something wrong with me, but please help me to understand what is happening....

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 1:34 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You are getting proxies, a very normal thing. Please start reading.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 1:40 pm 
Beginner
Beginner

Joined: Sat Nov 13, 2004 7:21 pm
Posts: 24
I'm sorry, but I read the documentation and I didn't see anything about proxies, at least in the HQL chapter....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 3:59 pm 
Beginner
Beginner

Joined: Fri Mar 12, 2004 8:40 pm
Posts: 30
Location: SF Bay Area
If you want House objects you must select only House objects.

So you're on the wrong page. You query must start with
"from House as house where" .

If your Person object has a reverse lookup House member
something like the hql below will.

Although there are probably better ways to do it.

Anyone ?

from House as house
where
house.house_id
in ( select person.House
from Person person where person.age < 20 )


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 5:44 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you can also hit the proxy (for example System.out.println(myObj.getProperty()) to see what happen.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.