1

Given the following HQL Query:

from Foo foo where foo.id in (:fooIds)

but here i have composite key in the Id ex we have two PK1 and pk2 as Id's.

How can we implement this query..

how can i pass both paramets in setparameters function of query

My question is similar this question

HBM file containing composite key is present below

<?xml version="1.0"?>

    <composite-id>
        <key-property name="foo1" column="FOO1" type="java.lang.String" length="36"/>
        <key-property name="foo2" column="FOO2" type="java.lang.Short" />       
    </composite-id>

    <property name="EffectiveDt" type="java.sql.Date"  column="EFFECTIVE_DT" />             
    <property name="effectiveTypeCd" type="java.lang.String" column="CERT_EFF_TYPE_CD" />
    <property name="statusCd" type="java.lang.String" column="CERT_STATUS_CD" />


</class>

Community
  • 1
  • 1
Vish
  • 794
  • 6
  • 17
  • 42

2 Answers2

1

Are you using a composite id? Do you have a separate class representing the composite-id or do you have 2 fields in Foo and you want to search using them in your query? Posting you Foo class would help!

octav
  • 1,151
  • 7
  • 6
  • we have 2 field in the the Foo class and we want to search using them.there is no separate class for composite ID – Vish Nov 02 '11 at 09:56
0

I'm not 100% sure you can use in in this case. One thing you can do is to build the query manually with something like

 String hqlQuery = "from Foo foo where "
 boolean first = true;
 for( ID id : fooids ) {
     if( first ) {
          hqlQuery += "foo.id = ?";
          first = false;
     } else {
          hqlQuery += " OR foo.id = ?";
     }
  }

  Query q = em.createQuery(hqlQuery);
  int position = 0;
  for( ID id : fooids ) {
      q.setParameter(position, id);
      position++;
  }

You might want to double check the code, as I'm writing it here, so there's a big chance there's a typo or two.

Augusto
  • 26,525
  • 5
  • 52
  • 82