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: How to set up query criteria for child objects.
PostPosted: Fri Dec 15, 2006 1:27 am 
Newbie

Joined: Mon Nov 20, 2006 1:53 am
Posts: 14
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

I am using nHibernate 1.2.0 and i am trying to query a bunch of objects that have child objects.

Mapping documents:

Say I have a class Bid which contains a unidirectional many to one relationship with another class Item.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="Test.ClientManagement.Model.Bid, Test.ClientManagement.Model" table="bid" lazy="false">
<cache usage="read-write"/>
<id name="Id" column="bid_id">
<generator class="increment" />
</id>
<property name="StartTime" column="start_ts" not-null="true"/>
<property name="BidDescription" column="bid_ds" not-null="true"/>
<many-to-one name="Item" column="item_id" class="Test.ClientManagement.Model.Item, Test.ClientManagement.Model"/>
</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

(here T is the type Bid)

ICriteria criteria = Session.Current.CreateCriteria(typeof(T))
.Add(Expression.Between("StartTime", new DateTime(2006, 1, 1, 9, 0, 0), new DateTime(2006, 1, 1, 21, 0, 0)));

criteria.SetCacheable(true);
criteria.SetTimeout(60);

return criteria.List();


Now this currently works fine but I want to add to the criteria that the Item name has to be like a certain string.

How do I add to this criteria to also match certain Item properties.

I tried adding an

.Add(Expression.Like("Item.Name", "Test%"));

to the code but this errors with:

"Additional information: could not resolve property: Item.ItemName of: Test.ClientManagement.Model.Bid"

Any help would be much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 7:30 am 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Code:
session.CreateCirteria(typeof(Big))
.Add(Expression.Between("StartTime", new DateTime(2006, 1, 1, 9, 0, 0), new DateTime(2006, 1, 1, 21, 0, 0)))
  .CreateCriteria("Item")
   .Add(Expression.Like("Name", "Test%"));


Top
 Profile  
 
 Post subject: Thank you!
PostPosted: Fri Dec 15, 2006 8:31 pm 
Newbie

Joined: Mon Nov 20, 2006 1:53 am
Posts: 14
Thanks very much for that! I will implement it Monday when i'm back at work and try it out thanks again so much for a speedy reply.


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.