Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resolve property: Id

I'm getting the following error message:

NHibernate.HibernateException: NHibernate.HibernateException: Unable to resolve property: Id.

This error is thrown from the following line of code:

User userFound = session.QueryOver<User>()
                   .Where(x => x.Id == testObjects.TestUser.Id)
                   .SingleOrDefault();

My abbreviated mappings are as follows:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("USER_HEADER");
        Id(x => x.Id, "USER_ID")
            .GeneratedBy.Foreign("UserLocation");

        HasOne(x => x.UserLocation)
            .PropertyRef(x => x.Id)
            .Cascade.All();
    }
}

public class LocationMap : ClassMap<Location>
{
    public LocationMap()
    {
        Table("LOC_HEADER");
        Id(x => x.Id, "LOC_ID");

        HasOne(x => x.User)
            .PropertyRef(x => x.Id);
    }
}

I was able to query a User object before I added this relationship to Location so I know it has something to do with it but I'm not sure what exactly. I can successfully create a User object that is tied to a Location but cannot query it. Using ISession.Get produces the same error as the above QueryOver statement.

Below is the overall unit test I am running that is failing:

    public void Can_Create_User()
    {
        using (NHibernate.ISession session = SessionFactory.GetCurrentSession())
        {
            using (NHibernate.ITransaction tran = session.BeginTransaction())
            {
                session.Save(testObjects.TestValidationDetail);
                session.Save(testObjects.TestUser);
                tran.Commit();
            }
        }

        using (NHibernate.ISession session = SessionFactory.GetCurrentSession())
        {
            User userFound = session.QueryOver<User>().Where(x => x.Id == testObjects.TestUser.Id).SingleOrDefault();

            Assert.IsNotNull(userFound);
            Assert.AreEqual(userFound.Id, userFound.UserLocation.Id);
        }
    }
like image 583
Cole W Avatar asked Sep 04 '25 04:09

Cole W


2 Answers

It turns out this was caused by me incorrectly using PropertyRef. In my instance I did not need to use this. The error was being generated because there was no property named Id but there was an ID named Id. I corrected my issues by changing my mappings to:

HasOne(x => x.UserLocation)
        .PropertyRef(x => x.Id)
        .Cascade.All();

to

HasOne(x => x.UserLocation)
        .Cascade.All();

and

HasOne(x => x.User)
        .PropertyRef(x => x.Id);

to

HasOne(x => x.User)
like image 191
Cole W Avatar answered Sep 06 '25 05:09

Cole W


PropertyRef maps to property-ref is a legacy feature, it is meant to allow you to create many-to-one associations when the association is not done on the primary key of the association.

I am guessing you want to specify on what property the join is to be made and that is why you used PropertyRef.. if you are using Nhibernates default convention in the mapping for the Id of UserLocation you dont need to explicitly specify the property.. if you are explicitly giving the column name then you need to do the same here, but in that case you need to specify the exact same column name.

Hope that helps..

like image 31
Baz1nga Avatar answered Sep 06 '25 05:09

Baz1nga