-->
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.  [ 7 posts ] 
Author Message
 Post subject: Criteria search with associations in RC1
PostPosted: Wed Sep 21, 2005 7:01 am 
Beginner
Beginner

Joined: Thu May 12, 2005 3:41 am
Posts: 24
Location: London, UK
I have encountered some problems using ICriteria with associations with the RC1 release. I am trying to return all Orders with a particular instance of Customer, where Customer is a many-to-one association in the Order class.

Extract from mapping files:
Code:
<class name="Customer" table="Customer">
  <id name="Id" unsaved-value="0">
    <generator class="identity" />
  </id>
  ...
</class>
<class name="Order" table="`Order`">
  <id name="Id" unsaved-value="0">
    <generator class="identity" />
  </id>
  <many-to-one name="Customer" column="CustomerID" class="Customer" />
  ...
</class>

The following code works with 0.9.1.0:
Code:
IList ordersForCustomer = session.CreateCriteria(typeof(Order))
  .Add(Expression.Eq("Customer", customer))
  .List();
In RC1, this throws the a QueryException with the message
Code:
cannot use association or collection (MyTests.Order.Customer) directly in a criterion, use ICriteria.CreateCriteria instead
I can make this work by using the following
Code:
ICriteria criteria = session.CreateCriteria(typeof(Order));
criteria.CreateCriteria("Customer")
  .Add(Expression.Eq("Id", customer.Id));
IList ordersForCustomer = criteria.List();

I would prefer to be able to use the first syntax for a number of reasons.
  • The second is more long-winded.
  • The first is more instinctive and more object-oriented. I want to look for orders that have a particular Customer object, but I am being forced to look for orders that have a Customer object with a particular Id property.
  • I have a number of instances in my code with the first syntax and I'd rather not have to change them all!
Two questions:
  1. Is there a reason why the old syntax is no longer supported?
  2. Is there a better way to achieve what I'm attempting?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 8:04 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Err, I tried to make criteria a bit more fool-proof and put an over-restrictive check in the code. I'll change it. Thanks for the report.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 11:42 am 
Newbie

Joined: Fri Jul 29, 2005 11:48 am
Posts: 11
I had the same exact problem when I upgraded and and came up with the same work around.

When do you think this will be fixed? I guess I'll revert back to the previous version untill then.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 4:33 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
It's fixed in CVS already, so it will be part of the next release. I don't think it's worth doing a separate release just for this bug fix, but if any more similar bugs show up, I will do it.


Top
 Profile  
 
 Post subject: Criteria search with associations in RC1
PostPosted: Wed Sep 21, 2005 5:26 pm 
Beginner
Beginner

Joined: Mon Sep 26, 2005 5:57 pm
Posts: 39
Well, this release has the feature to allow selection based on items in association collections, which is a widely expected feature.

I urge you to do a quick release.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 10:36 am 
Newbie

Joined: Fri Jul 29, 2005 11:48 am
Posts: 11
Are you talking about querying a property of a property? That would be great too.


Top
 Profile  
 
 Post subject: Second that quick release suggestion
PostPosted: Mon Sep 26, 2005 5:23 pm 
Newbie

Joined: Wed Jun 22, 2005 12:28 pm
Posts: 2
I too am faced with the choice between inserting the workaround for all the filters or reverting back to the previous version (I will likely go with the former). If the workaround is infact the more "proper" method then I have no issues and will convert but i have to agree with the above that the original way feels a bit more object oriented.

Thanks for the great update. Nhibernate has been a satisfying and immensly helpfull tool.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.