NHibernate Pitfalls: Get and Filters

This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.

This was suggested a long time ago by Kelly Brownsberger (@kbrowns). What happens is, even if you have filters defined, they are not applied on a call to ISession.Get<T>(). This is by design: you are explicitly asking for an entity with some id, so NHibernate just returns you that. On the other hand, static where restrictions defined at the entity level still apply.

                             

6 Comments

  • NH also has some other pitfalls with filters. Joined subclasses break and exception, which is a longstanding open bug: https://nhibernate.jira.com/browse/NH-2049

    Also, filters aren't applied by some queries, particularly IQueryable.

  • Hi, Chris! Thanks for your comment!
    Yes, NH-2049 is an oldie... maybe worth mentioning, yes!
    But what do you mean by "filters aren't applied by some queries, particularly IQueryable"? Can you provide an example or point me to a Jira issue?

  • I don't see the particular issue in JIRA right now, but the basic problem is with a class like this:

    public class SomeEntity
    {
    public virtual IList<SomeOtherEntity> OtherOnes { get; set; }
    }

    with a filter on OtherOnes like Deleted=false, say.

    Then you do Session.Query<SomeEntity>().SelectMany(x => x.OtherOnes) and it returns deleted ones. I'll have to spend some time and make a repro unit test for that.

    There are a lot of open issues around filters, like https://nhibernate.jira.com/browse/NH-3653, to the point where it's a minefield trying to use them -- you don't know if they're going to be applied or not, or if the SQL generated is going to be wrong and exception.

  • Chris,
    I believe the example you gave does work perfectly! I've been using something like that in production for quite a few years. Can you create an issue and include a failing test? Or send it to me by email (rjperes at hotmail).

  • Man, that's embarrassing -- I can't get it to repro now. I had what I thought was a repro a year ago or so, but I worked around it by removing the filter and manually adding criteria. That issue must have either been fixed since or it is caused by the same joined-subclass issue. I can't get an isolated test case to hit the same issue.

  • :-)
    It's better this way!
    Do let me hear your suggestions, I'll make sure to mention you as the author!

Add a Comment

As it will appear on the website

Not displayed

Your website