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: HQL to Criteria Query
PostPosted: Mon Apr 14, 2008 7:32 pm 
Newbie

Joined: Wed Apr 09, 2008 8:17 pm
Posts: 2
Hibernate version: 1.2.0
Databse: SQL Server 2005

I've been trying to replicate the following HQL Query using the Criteria API, with little success:
HQL Query:
Code:
select img from Image as img
inner join img.Details as details
where details.DetailType = 'Photographer'
  and :value = some elements(details.Values)


Which gives me all the images for which the photographer equals :value.

My Mapping documents are:
Code:
<class name="Test.Domain.Image, Test.Domain" table="Image">
    <id name="Id" column="Id" type="String" length="12" access="nosetter.camelcase-underscore" unsaved-value="null">
      <generator class="assigned" />
    </id>
  <!-- ... -->
    <map name="Details" table="ImageDetail" lazy="false" >
      <key column="ImageId"></key>
      <index column="DetailType" type="String" length="10"></index>
      <one-to-many class="Test.Domain.ImageDetailList, Test.Domain"/>
    </map>
  </class>
  <class name="Test.Domain.ImageDetailList, Test.Domain" table="ImageDetail">
    <id name="Id" column="DetailId" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="guid"></generator>
    </id>
    <property name="ImageId" column="ImageId" type="String" length="12" access="nosetter.camelcase-underscore"/>
    <property name="DetailType" column="DetailType" type="String" length="10" access="nosetter.camelcase-underscore"/>
    <set name="Values" table="ImageDetailValue">
      <key column="DetailId"></key>
      <element column="Value" type="string" length="50" unique="true" />
    </set>
  </class>



I've tried:
Code:
ICriteria crit = session.CreateCriteria(typeof(ImageDetailList));
crit.CreateAlias("Values", "v");
// Throws an Exception: collection was not an association: Test.Domain.ImageDetailList.Values


Any help would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 3:43 am 
Newbie

Joined: Sat Sep 22, 2007 5:42 am
Posts: 10
Hello!

Look at the documentation http://www.hibernate.org/hib_docs/nhibe ... a-criteria 12.4. Associations and for the "some elements" part a subquery wolud work
12.8. Detached queries and subqueries

One solution would be to create Criterias like this:

string theValue = "3";

DetachedCriteria query = DetachedCriteria.For(typeof(ImageDetailValue))
.Add( Expression.Eq("Value", theValue ) );


ICriteria crit = session.CreateCriteria(typeof(ImageDetailList))
.CreateCriteria("Details", JoinType.InnerJoin)
.Add( Expression.Eq("DetailType", "Photographer") )
.Add(Subqueries.Exsists("Values", query);

I am not sure but the last subquery part but hopefully I pointed you in teh right direction.

Regards[/url]


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.