-->
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.  [ 3 posts ] 
Author Message
 Post subject: help with a simple query
PostPosted: Wed Dec 07, 2005 5:22 pm 
Newbie

Joined: Wed Oct 19, 2005 12:19 pm
Posts: 4
hello every one,

I have read the the docs and searched in the forum but since I am new to hibernate, i had some difficulty in udnerstanding the concept.

Database tables : groups (groupId), users (userId) and groups_users (groupId, userId).

I have two hbm files and 2 POJOs each corresponding to groups.

    Do i need to have a POJO and a hbm.xml file for the link table whcih is group_user?
    How do I write a query to obtain all the users that belong to a group.

Following are the hbm.xml files. Thanks a ton for your time!
Code:

   <class name="pojo.Groups" table="GROUPS">   
        <id name="groupId" column="groupId">
            <generator class="increment"/>
        </id>
      <property name="groupName"/>
      
      <set name="users" table="GROUP_USER"
          inverse="true"
          cascade="save-update"
          lazy="true"
      >
           <key column="groupId"/>
           <many-to-many column="userId" class="pojo.Users"/>
       </set>
      

    </class>

----------------------------------------------------------
<class name="pojo.Users" table="USERS">
   
        <id name="userId" column="id">
            <generator class="increment"/>
        </id>

        <property name="login"/>
        <property name="firstName"/>       

        <property name="lastName"/>       

        <property name="phone"/>       
       
        <property name="email"/>       
       
        <property name="password"/>       

      <set name="groups" table="GROUP_USER"
          inverse="true"
          cascade="save-update"
          lazy="true">
           <key column="userId"/>
           <many-to-many column="groupId" class="pojo.Groups"/>
       </set>
    </class>
[/list]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 5:47 pm 
Beginner
Beginner

Joined: Fri Nov 04, 2005 3:51 pm
Posts: 32
You don't need a mapping and pojo setup for the many-to-many cross-ref table.

Assuming your mapping is ok, you should be able to do a query like...

Code:
select b from Groups a inner join a.users b where a.id = xxxx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 08, 2005 3:50 pm 
Newbie

Joined: Wed Oct 19, 2005 12:19 pm
Posts: 4
Thanks for your reply....
But just have couple of more question -

    1. my understanding is we are supposed to have a POJO for each table in teh database. how are we going to decide whether we need a POJO or not?
    2. How should I write the HQL to get all the users that do not belong to a group? This is what is tried

    Select u from Users u where u.userId not in (Select u.userId from Groups g inner join g.users u where g.groupId != "+ id +));


Thanks once again!


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