-->
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: WHERE condition on a Set
PostPosted: Mon Mar 29, 2004 5:22 am 
Beginner
Beginner

Joined: Fri Oct 17, 2003 4:11 am
Posts: 40
Hi!
I'm using Hibernate 2.1.1 and i'd like to select a Parent class with a specific Child class in a Set field.
It is possible or i must load all the Parent classes and then make a search (via Java language) on their Sets?

My classes are (just for example):
PARENT: id : Long, description : String, childs : Set
CHILD: id : Long, description : String

From a child istance "child1" (for example the Child class with id=1) can I retrive the parent classes that have Child class with id=1 in their set with a single query?

Thanx for any help!
Gio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 6:10 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
Look up the chapter on parent-child relationships in the hibernate manual. Especially note the points about bidirectional relationsships and the meaning of inverse="true".


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 6:50 am 
Beginner
Beginner

Joined: Fri Oct 17, 2003 4:11 am
Posts: 40
Thanx for your answer but i want to do that explained without bidirectional relation... just a query on the parent that can watch the Set....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 7:33 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
from Parent parent where :child in elements(parent.children)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 7:58 am 
Beginner
Beginner

Joined: Fri Oct 17, 2003 4:11 am
Posts: 40
Thanks very much for your answer... i use MySQL 4.1 and I get the following error (Gruppo is Parent, Permesso is the Child class)...

Code:
        Query query=session.createQuery("from HGruppo gruppo where :permesso in elements(gruppo.permessi)");
        query.setEntity("permesso",permesso);


Code:
4953 [main] DEBUG net.sf.hibernate.SQL  - select hgruppo0_.id as id, hgruppo0_.nome as nome, hgruppo0_.descrizione as descrizi3_ from ut_gruppo hgruppo0_ where (? in(select permessi1_.idPermesso from ut_gruppo_permesso permessi1_ where hgruppo0_.id=permessi1_.idGruppo))
Hibernate: select hgruppo0_.id as id, hgruppo0_.nome as nome, hgruppo0_.descrizione as descrizi3_ from ut_gruppo hgruppo0_ where (? in(select permessi1_.idPermesso from ut_gruppo_permesso permessi1_ where hgruppo0_.id=permessi1_.idGruppo))
4953 [main] DEBUG net.sf.hibernate.impl.BatcherImpl  - preparing statement
4953 [main] DEBUG net.sf.hibernate.type.LongType  - binding '19' to parameter: 1
4953 [main] DEBUG net.sf.hibernate.util.JDBCExceptionReporter  - SQL Exception
java.sql.SQLException: Syntax error or access violation,  message from server: "You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select permessi1_.idPermesso from ut_gruppo_permesso permessi1_"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 8:31 am 
Beginner
Beginner

Joined: Fri Oct 17, 2003 4:11 am
Posts: 40
DOH! I have MySQL 4.0 and maybe it doesn't work inner SELECT... :(
In your opinion can be this the problem?

I read that 4.1 is in Alpha... any idea about a solution?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 8:33 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use a better database, also read http://sql-info.de/mysql/gotchas.html if you like to use MySQL.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 29, 2004 9:16 am 
Beginner
Beginner

Joined: Fri Oct 17, 2003 4:11 am
Posts: 40
Ok!
I found a solution with join for my MySQL 4.0.13...

Code:
select parent from Parent parent inner join parent.children child where child = :child


Thanks to all!!


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.