4

I need to retrieve files with a status file = 10 and null values form a nullable VARCHAR2 column from an oracle db.

After some searching I found the following:

ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);  
criteria.Add(Expression.In("StatusFile", 10));
criteria.Add(Restrictions.IsEmpty("StatusFile"));

In sql would be something like:

select attstatus from table where file_tmode = 'P'and  (status is null or status = 10);

If I remove the last line, it works, but I have not been able to find a way to add the criteria for the null values.

How could I do this?

hikizume
  • 578
  • 11
  • 25

3 Answers3

4

Did you try IsNull?

NHibernateSession.CreateCriteria(persitentType)
  .Add(Expression.In("StatusFile", 10))
  .Add(Expression.IsNull("StatusFile"));

using or

NHibernateSession.CreateCriteria(persitentType)
  .Add(Expression.In("StatusFile", 10)
    || Expression.IsNull("StatusFile"));

Note that nulls are not indexed in oracle (at least a few years ago when I used it) and it may be very slow to find null values in a large table.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • This way does not throws an exception the way the one I had does, but nhibertate interprets it "StatusFile in (10) and StatusFile is null", and it does not retrieve any values. – hikizume Apr 17 '13 at 08:25
  • here is an explenation for an or: http://stackoverflow.com/questions/3299022/nhibernate-or-criteria-query – Verena Haunschmid Apr 17 '13 at 11:16
2
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);  
criteria.Add(Restrictions.Or (
    Restrictions.Eq ("StatusFile", 10), 
    Restrictions.IsNull ("StatusFile)
));
Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
0

.List() Since Stefan Steinegger's answer is 'and'ing your critieria, why don't you try disjunction ('or'ing)? Something like this, -

var query = Session.QueryOver<PersistentType>();
var disjunction = new Disjunction();   
disjunction.Add(Restrictions.On<PersistentType>(obj => obj.statusFile == 10));
disjunction.Add(Restrictions.On<PersistentType>(obj => obj.statusFile == null));
var queryResult = query.Where(disjunction).List<PersistentType>();

Or simply,

var queryResult = Session.QueryOver<PersistentType>()
    .Where(obj => obj.statusFile == 10 || obj.statusFile == null).List<PersistentType>();
mridula
  • 3,203
  • 3
  • 32
  • 55