How to return only one record from database using Nhibernate + Criteria + Firebird

criteriafirebirdnhibernate

I'm using Nhibernate 3.2, Firebird Net Provider 2.7 and Criteria. after googled to how return only one record in database i came to this…

var criteria = session.CreateCriteria(typeof(T))
    .SetFirstResult(0)
    .SetMaxResults(1)
    .Add(getRestricao(rest))
    .UniqueResult<T>();

tx.Commit();

return criteria;

But not works! The erros is:

NHibernate.Exceptions.GenericADOException : could not execute query
[ /* criteria query */ SELECT this_.idPermissao as idPermis1_21_0_, this_.nomePermissao as nomePerm2_21_0_, this_.permissao as permissao21_0_ FROM Permissao this_ WHERE this_.nomePermissao = ? ]
Name:cp0 - Value:Administrador
[SQL: /* criteria query */ SELECT this_.idPermissao as idPermis1_21_0_, this_.nomePermissao as nomePerm2_21_0_, this_.permissao as permissao21_0_ FROM Permissao this_ WHERE this_.nomePermissao = ?]
----> System.ArgumentException : index should be greater than or equal to 0
Parameter name: index

How to generate the "SELECT FIRST 1" from Firebird in Criteria? Thanks!

Best Answer

.UniqueResult should only return 1 record, if zero, or more than one records are returned then it throws an error.

If you run the SQL manually do you get more than one record?

Did you try changing .UniqueResult<T>() to .List<T>()

And then returning the first item from your method call.

return criteria.FirstOrDefault();

As long as you keep your max records then the first row should only be returned (or null)

Related Topic