Criteria query returns hydrated object in SQLite but not SqlServer

Posted by Berryl on Stack Overflow See other posts from Stack Overflow or by Berryl
Published on 2010-04-19T03:26:32Z Indexed on 2010/04/19 3:33 UTC
Read the original article Hit count: 278

Filed under:
|
|

I have a method that returns a resource fully hydrated when the db is SQLite but when the identical code is used by SqlServer the object is not fully hydrated. I'll explain that with the code after some brief background.

I my domain various otherwise unrelated things like an Employee or a Machine can be used as a Resource that can be allocated to. In the object model an example of this would be:

  /// <summary>Wraps a <see cref="StaffMember"/> in a <see cref="ResourceBase"/>. </summary>
public class StaffMemberResource : ResourceBase
{
    public virtual StaffMember StaffMember { get; private set; }

    public StaffMemberResource(StaffMember staffMember) {
        Check.RequireNotNull<StaffMember>(staffMember);
        base.BusinessId = staffMember.Number.ToString();
        base.Name = staffMember.Name.ToString();
        base.OrganizationName = staffMember.Department.Name;
        StaffMember = staffMember;
    }

    [UsedImplicitly]
    protected StaffMemberResource() { }
}

And in the db tables, there is a table per class inheritance where the ResourceBase has a discriminator and the id of the actual resource (ie, StaffMember)

StaffMember - 1 ---- M- ResourceBase - 1 ----- M - Allocation

The Code

    public override StaffMemberResource BuildResource(IActivityService activityService) {
        var sessionFactory = _GetSessionFactory();
        var session = sessionFactory.GetCurrentSession();
        StaffMemberResource result;
        using (var tx = session.BeginTransaction()) {
            var propertyName = ExprHelper.GetPropertyName<StaffMember>(x => x.Number);
            var staff = session.CreateCriteria<StaffMember>()
                .Add(Restrictions.Eq(propertyName, new EmployeeNumber(_testData.Resource_1.BusinessId)))
                .UniqueResult<StaffMember>();
            if (staff == null) {
                ... build up a staff member
                result = new StaffMemberResource(staff);
            }
            else {
        //////////
                var property = ExprHelper.GetPropertyName<StaffMemberResource>(x => x.StaffMember);
                result = session.CreateCriteria<StaffMemberResource>()
                    .Add(Restrictions.Eq(property, staff))
                    .UniqueResult<StaffMemberResource>();
            }
        ///////////
            tx.Commit();
        }
        return result;
    }

It's that second criteria query that works "properly" with SQLite but not with SqlServer. By properly I mean that the employee numer is translated into a ResourceBase.BusinessId, Name is flattened out into a ResourceBase.Name, etc.

Does anyone know why this might be?

Cheers, Berryl

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about criteria