-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Result list from Criteria Query contains null - why?
PostPosted: Thu Aug 09, 2007 8:25 pm 
Newbie

Joined: Thu Aug 09, 2007 7:25 pm
Posts: 2
Location: New Zealand
When I do a simple Criteria based query to load all Accounts in my system, the resulting list contains a null value as the last item in the list (at index 3968).

I've simplified the code I'm using down to this:

Code:
    ICriteria c = q.CreateQuery(typeof(Account));
    IList<Account> results = c.List<Account>();



I thought that the list returned from this would never contain nulls - but in this case, a null is returned, and I don't understand why.

Taking the generated SQL and running it manually against the database works, and I don't see anything out of place in the resultset - but, then again, I'm not entirely sure what to look for.

Any help or suggestions on where to look would be greatfully received.

Hibernate version: 1.2.0 GA

Mapping document:

The object is pretty standalone, with no outgoing associations. There is one inbound association, from a person table.

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="RBNZ.RBContacts.Domain"
                   assembly="RBNZ.RBContacts.Domain"
                   default-lazy="false">

  <class name="Account"
         table="accounts"
         lazy="false">

    <id name="AccountId">
        <generator class="identity"/>
    </id>
   
    <property name="AccountId" column="AccountId"/>
    <property name="Name" column="Account"/>
    <property name="Type" column="Type"/>
    <property name="Status" column="Status"/>
    <property name="Street" column="Street"/>
    <property name="City" column="City"/>
   
  </class>
 
</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():

Code:
using (IQueryContext q = mDatabase.NewQueryContext())
{
    ICriteria c = q.CreateQuery(typeof(Account));
    IList<Account> results = c.List<Account>();
    return results;
}



The QueryContext object is a local wrapper that enforces some local conventions. Lots of other queries in our system don't have the problem I'm experiencing here, so I believe that the problem is elsewhere.

However, here are the relevant bits of it's implementation in case I'm mistaken. The ancestor class (QueryContext) contains nothing NHibernate specific - mostly, it just implements IDisposable.

Code:
public class NHibernateQueryContext : QueryContext
{
    public ISession Session
    {
        get { return mSession; }
    }

    public NHibernateQueryContext(ISessionFactory aSessionFactory)
        : base()
    {
        mSession = aSessionFactory.OpenSession();
    }

    protected override void DisposeManaged()
    {
        mSession.Dispose();

        base.DisposeManaged();
    }

    public override ICriteria CreateQuery(Type aType)
    {
        return mSession.CreateCriteria(aType);
    }

    public override ICriteria CreateQuery(NHibernate.Expression.DetachedCriteria aQuery)
    {
        return aQuery.GetExecutableCriteria(mSession);
    }

    private ISession mSession;
}



Name and version of the database you are using:

Running against MS SQL Server 2005. The same behaviour occurs against both Express and Enterprise editions, though in both cases they're containing the same data (copied from a production system).

The generated SQL (from the Log4Net logfile):

Code:
SELECT this_.AccountId as AccountId0_0_,
       this_.Account as Account0_0_,
       this_.Type as Type0_0_,
       this_.Status as Status0_0_,
       this_.Street as Street0_0_,
       this_.City as City0_0_
FROM accounts this_



Top
 Profile  
 
 Post subject: Result list from Criteria Query contains null - Solution!
PostPosted: Thu Nov 15, 2007 4:58 pm 
Newbie

Joined: Thu Aug 09, 2007 7:25 pm
Posts: 2
Location: New Zealand
After using a nasty workaround for some time, I finally found the reason for the "null" showing up in my result list:

One of the rows in the database had a null value for the primary key column!

It appears (though I haven't traced the source to be sure) that a null primary key turns into a null entity.

Counter intuitive (to me, anyway), but at least I now have an explanation for the behaviour I was seeing.

While I can't change the database (it's not under my control), I can now workaround the issue confident that the workaround is stable.

Hope someone finds this helpful,
Bevan.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.