-->
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: HQL: How to check if an object contains all the ids in list
PostPosted: Tue Dec 18, 2007 9:48 pm 
Beginner
Beginner

Joined: Tue Aug 01, 2006 4:57 am
Posts: 25
Hibernate version:3.2

Hi, I am trying to write a named query where the query should return list of objects which contain all the ids in the passed parameterList.

I am new to SQL and HQL, so i could not find a right way to do this. Please help, here is the situation:

Object A and B have many to many relationship. I am trying to write a named query wherein I can pass list of 'ids of B' as parameter. the query should return list of A which contain all the values in paramterList.

How do I achieve this? Please help... :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 18, 2007 10:10 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
I believe you can't although I would loved to be proved wrong as that is one of my pet hates.

If your query can be expressed using the Hibernate Criteria API you can use the 'Expression.in(<fieldName>, <collection>)' API to build the IN clause of your query. If the query cannot be expressed using the Hibernate Criteria API, for example if it contains an explicit join, you need to build up the query yourself as a string adding the arguments for the IN clause in a loop.


Top
 Profile  
 
 Post subject: Re: HQL: How to check if an object contains all the ids in l
PostPosted: Wed Dec 19, 2007 1:29 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
GameOne wrote:
Hibernate version:3.2

Hi, I am trying to write a named query where the query should return list of objects which contain all the ids in the passed parameterList.

I am new to SQL and HQL, so i could not find a right way to do this. Please help, here is the situation:

Object A and B have many to many relationship. I am trying to write a named query wherein I can pass list of 'ids of B' as parameter. the query should return list of A which contain all the values in paramterList.

How do I achieve this? Please help... :(


I did a little prototype that resembles your situation. Imagine a case of person and address many-to-many relationship. You can define a query to get a list of people who are associated to a certain list of addresses. The query would look like:

Code:
select distinct p from Person p inner join p.addresses address where address.address in ('300 Somewhere', '232 Here')
            and size(p.addresses) = 2


The mapping for Person class is:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="test.model.data.Person" table="Person">

        <id name="id" type="long" unsaved-value="null">
            <column name="PERSONID"/>
            <generator class="native"/>
        </id>

        <property name="name" type="string">
            <column name="NAME" length="50" not-null="false"/>
        </property>

        <set name="addresses" cascade="all" table="PERSONADDRESS" inverse="false">
            <key column="PERSIONID"/>
            <many-to-many column="ADDRESSID" class="test.model.data.Address" />
        </set>
    </class>

    <query name="test.model.data.Person.find" cacheable="true" cache-region="com.necho.platform.model.data.Entity.findByName">
        <![CDATA[
            select distinct p from Person p inner join p.addresses address where address.address in ('300 Somewhere', '232 Here')
            and size(p.addresses) = 2
        ]]>
    </query>

</hibernate-mapping>


and the mapping for Address is fairly straight forward:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="test.model.data.Address" table="ADDRESS">

        <id name="id" type="long" unsaved-value="null">
            <column name="ADDRESSID"/>
            <generator class="native"/>
        </id>

        <property name="address" type="string">
            <column name="TEXT" length="150" not-null="false"/>
        </property>
    </class>

</hibernate-mapping>


Note that the list of addresses and the size can be a parameter. I hope this helps.


Farzad-


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.