Is there an NHibernate criterion that actually means nothing?

I am looking for an NHibernate criterion that does not add restriction on the criteria. The reason for this is because I have a method that converts some input parameters into a criterion that is added to the criteria. There is a constellation of input parameters in which no limitation is required. Therefore, I want to return some kind of fictitious criterion.

Is there something similar in NHibernate?

Best regards,
Oliver Hanappi

+3
source share
3 answers

, - Restrictions.IsNotNull( "id" ), "id" ( ). , , .

0

Conjuction, ( "1 = 1" ).

.

ICriterion conditionalCriteria = includeCriteria 
    ? Restrictions.Eq("someEntity.Field", variable)
    : (ICriterion) Restrictions.Conjuction();

var query = Session
    .CreateCriteria<SomeEntity>("someEntity")
    .Add(conditionalCriteria)
    .SetResultTransformer(Transformers.AliasToBean<SomeEntity>())
    .List<SomeEntity>();
+3

You can add criteria as needed, so just check your parameters for a null value, if they are not equal to zero, then add the criteria. Example:

Criteria cr = session.createCriteria(Employee.class)
cr.add(Restrictions.like("firstName", "Bob%"));

if (par_salary.IsNotNullOrEmpty())
{
     cr.add(Restrictions.eq("salary", par_salary));
}
List results = cr.list();

part of this is taken from Hibernation Requests

0
source

Source: https://habr.com/ru/post/1713320/


All Articles