-->
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.  [ 1 post ] 
Author Message
 Post subject: How to select from many to many.
PostPosted: Thu Nov 14, 2013 4:42 pm 
Newbie

Joined: Sat Aug 25, 2012 3:18 pm
Posts: 4
Hi.

I can't write hql query which select from Users and Groups like sql:
Code:
SELECT u.user_id, u.username, g.group_name  FROM users u, user_groups ug,
groups g WHERE u.user_id = ug.user_id AND ug.group_id = g.group_id


Users.hbm.xml
Code:
<hibernate-mapping>
    <class name="model.Users" table="users">
    <id name="user_id">
        <generator class="identity" />
    </id>
    <property name="username" type="string" />
    <property name="first_name" type="string" />
    <property name="middle_name" type="string" />
    <property name="last_name" type="string" />
    <property name="password" type="string" />
    <set name="groups" table="user_groups" inverse="false" lazy="true" fetch="select" cascade="all" >
        <key>
            <column name="user_id" not-null="true" />
        </key>
        <many-to-many entity-name="model.Groups">
            <column name="group_id" not-null="true" />
        </many-to-many>
    </set>
    <!--one to many users -> incydent -->
    <set name="incydents" table="incydents" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="user_id" not-null="true" />
            </key>
            <one-to-many class="model.Incydents" />
    </set>
    <!-- one to many users -> incydent -->
    </class>
</hibernate-mapping>


Code:
Groups.hbm.xml

<hibernate-mapping>
    <class name="model.Groups" table="groups">
  <id name="group_id">
      <column name="group_id" />
      <generator class="identity" />
  </id>
  <property name="group_name" />
  <property name="group_desc" />
  <set name="users" table="user_groups" inverse="true" lazy="true" fetch="select">
      <key>
          <column name="group_id" not-null="true" />
      </key>
      <many-to-many entity-name="model.Users">
          <column name="user_id" not-null="true" />
      </many-to-many>
  </set>
  </class>
</hibernate-mapping>


Incydents.hbm.xml

Code:
<hibernate-mapping>
    <class name="model.Incydents" table="incydents">
        <id name="inc_id" column="inc_id">
            <generator class="identity" /> <!--native-->
        </id>
        <property name="inc_desc" />
        <many-to-one name="users" class="model.Users" fetch="select">
            <column name="user_id" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>


Thx for all ;-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.