Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nhibernate GetById returns ObjectNotFoundException insetad of null

I am using fluent Nhibernate. This code Loads an instance of type T from the DB based on its ID.

public T GetById(IdT id, bool shouldLock)
    {
        T entity;

        if (shouldLock)
        {
            entity = (T) NHibernateSession.Load(persitentType, id, LockMode.Upgrade);
        }
        else
        {
            entity = (T) NHibernateSession.Load(persitentType, id);
        }

        return entity;
    }

But I have big problem. When I call property on it I get ObjectNotFoundException instead of null.

How can I make that entity be nullable and not return the Exception?

like image 543
senzacionale Avatar asked Oct 25 '10 18:10

senzacionale


1 Answers

I would use Get instead of Load. Get will return null, instead of an exception.

like image 90
Kevin Stricker Avatar answered Nov 06 '22 14:11

Kevin Stricker