-->
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.  [ 2 posts ] 
Author Message
 Post subject: Query on association
PostPosted: Wed May 19, 2004 7:23 pm 
Newbie

Joined: Mon Sep 15, 2003 9:09 am
Posts: 9
I have four tables (mappings below):

DATABASE
ORGANIZATION
SITE_USER
SITE_USER_ORGANIZATION

There is a one-to-many relationship between ORGANIZATION and DATABASE, and a many-to-many relationship between ORGANIZATION and SITE_USER. I want to construct an HQL query that returns all of the databases for a given site user. In SQL, I would write:

Code:
select d.*
  from database d, site_user_organization suo
where d.organization_id = suo.organization_id
    and suo.site_user_id = :suid;

What is the right way to write an HQL query to retrieve the same result? I've been through the documentation repeatedly; where in the docs would I get a clue as how to do this? Any help with this is much appreciated....

Hibernate version is 2.1.2
Database is Oracle9i

Mappings:

DATABASE
Code:
<hibernate-mapping package="com.twocoast.tcsc.model">
  <class name="Database" table="DATABASE">
    <id name="databaseId" column="DATABASE_ID" type="long" unsaved-value="0">
      <generator class="sequence">
        <param name="sequence">OBJECT_SEQ</param>
      </generator>
    </id>
    <property name="databaseName" column="DATABASE_NAME"/>
</hibernate-mapping>


ORGANIZATION
Code:
<hibernate-mapping package="com.twocoast.tcsc.model">
  <class name="Organization" table="SUPPORTED_ORGANIZATION">
    <id name="organizationId" column="ORGANIZATION_ID" type="long" unsaved-value="0">
      <generator class="sequence">
        <param name="sequence">OBJECT_SEQ</param>
      </generator>
    </id>
    <property name="organizationShortName" column="ORGANIZATION_SHORT_NAME"/>
    <set name="databases" inverse="true" lazy="true" cascade="all-delete-orphan" order-by="database_name asc">
      <key column="organization_id"/>
      <one-to-many class="Database"/>
    </set>   
  </class>
</hibernate-mapping>


SITE_USER
Code:
<hibernate-mapping package="com.twocoast.tcsc.model">
  <class name="SiteUser" table="SITE_USER">
    <id name="siteUserId" column="SITE_USER_ID" type="long" unsaved-value="0">
      <generator class="sequence">
        <param name="sequence">SITE_USER_SEQ</param>
      </generator>
    </id>
    <property name="username" column="USERNAME"/>
    <set name="organizations" table="SITE_USER_ORGANIZATION" lazy="true">
      <key column="SITE_USER_ID"/>
      <many-to-many column="ORGANIZATION_ID" class="Organization"/>
    </set>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 20, 2004 5:16 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Code:
select elements(org.databases)
from organization org, siteUser su
where su.uId = xxx
and org in elements(su.organizations)


if you want distinct databases, you can construct a new hashSet from this query result.

the HQL above can generate a subselect and be less performant than the SQL you've written.
If you need higher performance take a look at "SQLQuery" in the doc, in your case it will be very easy.


Anthony


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