-->
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: Need help translating HQL with 'exists' into Criteria query
PostPosted: Tue Feb 24, 2015 6:32 pm 
Newbie

Joined: Tue Feb 24, 2015 5:57 pm
Posts: 1
I have the following query in HQL that I'm struggling to convert into a Hibernate Criteria query:

Query (*Note: Number of properties queried can vary)
------------------------------------------------------
select e from Entity e
where
true
and exists (from e.props as p where p.name = 'a' and p.value='1')
and exists (from e.props as p where p.name = 'b' and p.value='2')

Data-Model
------------
<hibernate-mapping>
<class name="com.x.Entity" table="ENTITY">
<id name="id" column="ID">
<generator class="native" />
</id>

<property name="name" column="NAME" type="string"/>
<set name="props">
<key column="entity_id"/>
<one-to-many class="com.x.Property"/>
</set>
</class>

<class name="com.x.Property" table="PROPERTY">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name" type="string"/>
<property name="value" column="value" type="string"/>
</class>
</hibernate-mapping>




Example Data
------------------------------------------------------
Entity 1:
name: ent1-a1-b2,
Properties (name,value):
("a", "1")
("b", "2")

Entity 2:
name: ent1-a1-c3,
Properties (name,value):
("a", "1")
("c", "3")

Entity 3:
name: ent1-b2-c3,
Properties (name,value):
("b", "2")
("c", "3")


Result of query in this case should return the Entity "ent1-a1-b2".


Thanks in advance


Top
 Profile  
 
 Post subject: Re: Need help translating HQL with 'exists' into Criteria query
PostPosted: Wed Feb 25, 2015 6:16 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
I'm not sure how to create that particular query.

It should be something like:

Code:
session.createCriteria( Entity.class, "e" )
         .add( Subqueries.exists( DetachedCriteria.forClass(Entity.class)
               .createAlias("e.props", "p")
                   .add( Restrictions.and( Restrictions.eqProperty( "p.name", "a" ), Restrictions.eqProperty( "p.value", "1" )) ) )
             )
          .add( Subqueries.exists( DetachedCriteria.forClass(Entity.class)
               .createAlias("e.props", "p")
                   .add( Restrictions.and( Restrictions.eqProperty( "p.name", "b" ), Restrictions.eqProperty( "p.value", "2" )) ) )
             )
      ;


But isn't your query equivalent to:

Code:
SELECT e
FROM Entity e INNER JOIN e.props p
WHERE (p.name = 'a' AND p.value='1') OR (p.name = 'b' AND p.value='2')


the criteria equivalent would be:
Code:
session.createCriteria( Entity.class )
            .createAlias( "props", "p" )
               .add( Restrictions.or(
                     Restrictions.and( Restrictions.eqProperty( "p.name", "a" ), Restrictions.eqProperty( "p.value", "1" ) ),
                     Restrictions.and( Restrictions.eqProperty( "p.name", "b" ), Restrictions.eqProperty( "p.value", "2" ) )
                  )
               )
            ;


Note that I haven't tested these queries so I'm not sure they will work.

Hope this help,
Davide


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.