-->
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: Criteria based query with no constraints on association
PostPosted: Wed Apr 26, 2006 12:33 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Given the following tables with no relation between them.


TableA ( a_key1, a_col2, a_col3, a_col4 )

TableB ( b_key1, a_key1, b_col3, b_col4 )

mapping files:
TableA.xml
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="TableA">
      <id name="a_key1" column="a_key1"/>
     
      <property name="a_col2" column="a_col2"/>
      <property name="a_col3" column="a_col3"/>
      <property name="a_col4" column="a_col4"/>

      <set name="bSet"
            inverse="true" lazy="true"
            cascade="all-delete-orphan">
         <key>
            <column name="akey_1"/>
         </key>
         <one-to-many class="TableB"/>
      </bag>
   </class>

</hibernate-mapping>



TableB.xml
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="TableB">
      <composite-id>
         <key-property name="b_key1" column="b_key1"/>
         <key-property name="a_key1" column="a_key1"/>
      </composite-id>
     
      <property name="b_col2" column="b_col2"/>
      <property name="b_col3" column="b_col3"/>
      <property name="b_col4" column="b_col4"/>

      <many-to-one name="aObject" class="TableA" column="a_key1" insert="false" update="false" not-null="true"/>
   </class>

</hibernate-mapping>



The Java POJO classes are as expected with TableB implementing Serializable interface as required by <composite-id> feature.

And the result I would like to have from a similar sql query as shown below

Code:
select distinct( a.a_key1 ), b.key1
from TableA a, TableB b
where b.b_key1 = 'Some_Value'
and   b.a_key1 in (  select a2.a_key1 from TableA a2 );


is achieved by using the following Criteria code

Code:
Criteria c = session.createCriteria( TableB.class ).setFetchMode( "aObject", FetchMode.JOIN )
                        .createAlias( "aObject", "a_alias" );
c = c.add( Restrictions.eq( "b_key1", "Some_Value" ) );
c = c.add( Restrictions.eqProperty( "this.a_key1", "a_alias.a_key1" ) );


I am still not sure if the approach is correct, but atleast I have the result.

For information sake, the generated sql query from hibernate logs is as follows

Code:
  select a.a_key1, a.a_col2, a.a_col3, a.a_col4
         b.b_key1, b.a_key1, b.b_col3, b.b_col4
  from TableB b
  inner join TableA a on b.a_key1 = a.a_key1
  where b.b_key1 = 'Some_Value'
  and   b.a_key1 = a.a_key1


---------------------

However, when I want to get the result from an sql query as like below

Code:
select distinct( a.a_key1 ), b.key1
from TableA a, TableB b
where b.b_key1 = 'Some_Value'
and  b.a_key1 not in (  select a2.a_key1 from TableA a2 );


the following Criteria query is not giving me expected result

Code:
Criteria c = session.createCriteria( TableB.class ).setFetchMode( "aObject", FetchMode.JOIN )
                        .createAlias( "aObject", "a_alias" );
c = c.add( Restrictions.eq( "b_key1", "Some_Value" ) );
c = c.add( Restrictions.neProperty( "this.a_key1", "a_alias.a_key1" ) );


And the generates SQL query from the hibernate logs is as below

Code:
  select a.a_key1, a.a_col2, a.a_col3, a.a_col4
         b.b_key1, b.a_key1, b.b_col3, b.b_col4
  from TableB b
  inner join TableA a on b.a_key1 = a.a_key1
  where b.b_key1 = 'Some_Value'
  and   b.a_key1 <> a.a_key1


Can anyone give me the solution from Criteria approach instead of HQL or SQL approach.
Thanks in advance and appreciate it much as it helps a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 2:15 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
This is pretty much exactly what the last example in section 15.8, "Detached queries and subqueries" of the ref docs is all about. If you can't get further after re-reading that, post again and I'll have another look in the morning.


Top
 Profile  
 
 Post subject: Re: Criteria based query with no constraints on association
PostPosted: Wed Apr 26, 2006 10:31 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Thank you for pointing me to the documentation link and it helped me in getting the queries right.

Also I modified my code as follows if anyone is interested

Code:
DetachedCriteria dc = DetachedCriteria.forClass( TableA.class , "tablea");
dc.setProjection( Property.forName("tablea.a_key1") );

Criteria c = session.createCriteria( TableB.class, "tableb" );
c = c.add( Restrictions.eq( "tableb.b_key1", "Some_Value" ) );
c = c.add( Property.forName( "tableb.a_key1" ).notIn( dc ) );

List results = c.list();


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.