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: Criterion/Criteria questions
PostPosted: Sun Mar 02, 2008 2:39 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
Is there such a thing as a "Nop" ICriterion ? (as in no operation)

I am also having issues trying to understand how to get a row count projection COUNT(*) as I only want the count value out of the query. The following code does not compile as I am not specifying I want RowCountProjection correctly and google is not being my friend today (I cant find a single working example so far for NHibernate, just plenty of API docs with no examples :( ).

I want to optionally add ICriterion into a statement depending upon the callers requested configuration, like the following:

Code:
ICriterion foobarOptionalOne = Expression.Nop();
if(someConditionOne)
    foobarOptionalOne = Expression.Eq("EntityPropertyOne", 42);

ICriterion foobarOptionalTwo = Expression.Nop();
if(someConditionTwo)
    foobarOptionalTwo = Expression.Eq("EntityPropertyTwo", 42);

Int32 c = session.CreateCriteria(typeof(MyClass))
    .SetProjection(new RowCountProjection()  /* EUH ??? */ )
    .Add(Expression.Eq("FixedRequirement", 99)
    .Add(foobarOptionalOne)
    .Add(foobarOptionalTwo)
    ).UniqueResult<int>();


From looking at the source and documentation it does not seem allowed to call .Add(null) which is why I'm wanting to know if there is a Expression.Nop() placeholder ?

TIA!


Top
 Profile  
 
 Post subject: Criterion/Criteria questions
PostPosted: Sun Mar 02, 2008 4:58 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I don't think there is a 'nop' criterion. However, another way of structuring the same code might be:

Code:

ICriteria query = session.CreateCriteria(typeof(MyClass));

if(someConditionOne)
    query.Add(Expression.Eq("EntityPropertyOne", 42));

if(someConditionTwo)
    query.Add(Expression.Eq("EntityPropertyTwo", 42));

Int32 c = query
    .SetProjection(Projections.RowCount())
    .Add(Expression.Eq("FixedRequirement", 99))
    .UniqueResult<int>();



Does that help at all?

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 02, 2008 9:52 pm 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
Duh! Thanks very much thats tidy enough for my code base.


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.