-->
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.  [ 6 posts ] 
Author Message
 Post subject: navigating n-m relation in hql
PostPosted: Fri Apr 22, 2005 4:52 am 
JBoss jBPM Developer
JBoss jBPM Developer

Joined: Thu Aug 28, 2003 11:56 am
Posts: 30
Location: Belgium
Hibernate version: 3.0 final

Hsqldb version: 1.7.3.3

Problem: the query generated by hibernate does not work on hsqldb.

Error msg: (below is a formatted version of the SQL-query) java.sql.SQLException: Column not found: ID in statement [select user0_.id as id, user0_.name as name0_ from USER user0_, ROLE role1_ where (role1_.name='admin' and (user0_.id in (select users2_.USER from USER_ROLES users2_ where role1_.id=users2_.ROLE)))]

Questions:
* is this a problem in my code, hsqldb or hibernate ? (ordered by likeliness:-)
* is there a way that hibernate generates join-queries instead of subselects for navigating n-m relations ? i only found the "in elements(collection-property)" to be used in where clauses, which generates a subselect.

The minimal example i created to reproduce the problem models the n-m relation between users and roles.

HQL query:
Code:
Query q = s.createQuery(
  "select u " +
  "from org.test.User as u," +
  "     org.test.Role as r " +
  "where r.name = 'admin'" +
  "  and u in elements(r.users)" );
q.list();


Generated SQL:
Code:
select
  user0_.id as id,
  user0_.name as name0_

from
  USER user0_,
  ROLE role1_

where
( role1_.name='admin'
  and (user0_.id in (select users2_.USER
                     from USER_ROLES users2_
                     where role1_.id=users2_.ROLE
                    )
       )
)


Classes:
Code:
public class User {
  long id;
  String name;
  Set roles = new HashSet();
  ...
}

public class Role {
  long id;
  String name;
  Set users = new HashSet();
  ...
}


Mapping documents:
Code:
  <class name="org.test.User"
         table="USER" >
    <id name="id"><generator class="native" /></id>
    <property name="name" />
    <set name="roles"
         table="USER_ROLES"
         inverse="true"
         cascade="all" >
      <key column="USER" />
      <many-to-many class="org.test.Role" column="ROLE" />
    </set>
  </class>

  <class name="org.test.Role"
         table="ROLE" >
    <id name="id"><generator class="native" /></id>
    <property name="name" />
    <set name="users"
         table="USER_ROLES">
      <key column="ROLE" />
      <many-to-many class="org.test.User" column="USER" />
    </set>
  </class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 6:36 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
You didn't specify the column attribute of the <id>. Does the User table have a ID column ?

_________________
--
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 6:41 am 
JBoss jBPM Developer
JBoss jBPM Developer

Joined: Thu Aug 28, 2003 11:56 am
Posts: 30
Location: Belgium
yes, USER has an id column.

(by default hibernate takes the name of the id property as the column name)

regards, tom.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 7:05 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
The only thing I can suggest is to avoid User as table/column name, since it's a reserved word in sql.

_________________
--
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 11:06 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
how about
Code:
select distinct u from Role r join r.users u where r.name='admin'


And avoid tables named User, you'll be in trouble for portability.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 11:30 am 
JBoss jBPM Developer
JBoss jBPM Developer

Joined: Thu Aug 28, 2003 11:56 am
Posts: 30
Location: Belgium
exactly what i needed. thanks !


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