-->
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.  [ 8 posts ] 
Author Message
 Post subject: Select entries with status.name does not equal "file&am
PostPosted: Thu Oct 19, 2006 1:07 pm 
Beginner
Beginner

Joined: Thu Oct 19, 2006 1:03 pm
Posts: 29
Hibernate version:
Production version

Mapping documents:
3 of them

Code between sessionFactory.openSession() and session.close():
Code:
session = factory.OpenSession();
results = session.CreateCriteria(typeof(Entry))
   .Add(Expression.Lt("ExpiryDate", DateTime.Now))
   .Add( Expression.Not( Expression.Equals("Status.Name", "File"))   )
   .Add(Order.Asc("ExpiryDate"))
   .List();


Full stack trace of any exception that occurs:
-

Name and version of the database you are using:
MySql 5

I want to select all entries that have not:
myEntry.Status.Name = "File"
i.e. those entries whose statuses are not equal to "file".

How I do that, man...! Help!!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 1:18 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Code:
results = session.CreateCriteria(typeof(Entry))
   .Add(Expression.Lt("ExpiryDate", DateTime.Now))
   .Add(Order.Asc("ExpiryDate"))
   .CreateCriteria("Status")
     .Add( Expression.Not( Expression.Equals("Name", "File"))   )
   .List();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 1:47 pm 
Beginner
Beginner

Joined: Thu Oct 19, 2006 1:03 pm
Posts: 29
Hi Ananasi,

OK, so I tried that:
Error 1 The best overloaded method match for 'NHibernate.ICriteria.Add(NHibernate.Expression.ICriterion)' has some invalid arguments
(marks second Add)

Error 2 Argument '1': cannot convert from 'NHibernate.Expression.Order' to 'NHibernate.Expression.ICriterion'
(marks Order)

Error 3 The best overloaded method match for 'NHibernate.Expression.Expression.Not(NHibernate.Expression.ICriterion)' has some invalid arguments
(marks Expression in third Add)


Error 4 Argument '1': cannot convert from 'bool' to 'NHibernate.Expression.ICriterion' D:\WD\fileitfirst\App_Code\BusinessLayer.EntriesBLL.cs 258 27 D:\WD\fileitfirst\
(marks Expression in Not)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 2:07 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Sorry, mixed java and .net a smidge there. The
Code:
.Add(Order.Asc("ExpiryDate"))
should be
Code:
.AddOrder(Order.Asc("ExpiryDate"))


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 2:27 pm 
Beginner
Beginner

Joined: Thu Oct 19, 2006 1:03 pm
Posts: 29
Halfed the errors;

Error 1 The best overloaded method match for 'NHibernate.Expression.Expression.Not(NHibernate.Expression.ICriterion)' has some invalid arguments

Error 2 Argument '1': cannot convert from 'bool' to 'NHibernate.Expression.ICriterion'


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 2:46 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Sorry, I'm not as proficient in .net syntax, but I'm sure the problem has to do with the method chaining. Try this:
Code:
ICriteria criteria = session.CreateCriteria(typeof(Entry));
criteria.Add( Expression.Lt("ExpiryDate", DateTime.Now));
criteria.AddOrder(Order.Asc("ExpiryDate"));

ICriteria subSelect = criteria.CreateCriteria("Status");
ICriterion exclude = Expression.Eq("Name", "File");
subSelect.Add( Expression.Not ( exclude ) );
results = criteria.List();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 8:34 pm 
Beginner
Beginner

Joined: Thu Oct 19, 2006 1:03 pm
Posts: 29
Thanks! That solved it :)

So from your example: the chaining:
Code:
results = session.CreateCriteria(typeof(Entry))
   .Add(Expression.Lt("ExpiryDate", DateTime.Now))
   .AddOrder(Order.Asc("ExpiryDate"))
   .CreateCriteria("Status")
      .Add(Expression.Not(Expression.Eq("Name", "File")))
.List();


The tiny difference here is that .Equals is a method that always is possible to chain onto any object in the .Net environment. Hence .Eq is what works.

Just a quick question - does this mean that I fetch the whole data set and thereafter filter it, or are the entries filtered via SQL; before being fetched by NHibernate from the data store?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 10:23 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Good! I am in the middle of learning .net (java background), so my syntax is still a bit rough. The entities should be filtered via SQL. I am assuming that Status is a one-to-one relationship. If it is a collection of Statuses, then that associated collection would be returned unfiltered, but can be prefiltered using a ResultTransformer as in the third example of http://www.hibernate.org/hib_docs/nhibe ... sociations . HTH.


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