views:

141

answers:

2

I'm feeling dumb.

public class Uber
{
   public Foo Foo { get; set; }
   public Bar Bar { get; set; }
}

public class Foo
{
   public string Name { get; set; }
}

...

var ubercharged = session.CreateCriteria(typeof(Uber))
.Add(Expression.Eq("Foo.Name", "somename"))
.UniqueResult<Uber>();
return ubercharged;

This throws a "could not resolve property" error.

What am I doing wrong? I want to query for an Uber object that has a property Foo which has a Name of "somename".

updated with real life example, repository call, using fluent nhibernate:

    public UserPersonalization GetUserPersonalization(string username)
    {
        ISession session = _sessionSource.GetSession();

        var personuser = session.CreateCriteria(typeof(UserPersonalization))
            .Add(Expression.Eq("User.Username", username))
            .UniqueResult<UserPersonalization>();
        return personuser;

    }

The classes/mappings:

public class User
{
    public virtual Guid UserId { get; set; }
    public virtual string Username { get; set; }
    public virtual string Email { get; set; }
    public virtual string PasswordHash { get; set; }
    public virtual string PasswordSalt { get; set; }
    public virtual bool IsLockedOut { get; set; }
    public virtual bool IsApproved { get; set; }

}

public class Person
{
    public virtual int PersonId { get; set; }
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }

}

public class UserPersonalization
{
    public virtual int UserPersonalizationId { get; set; }
    public virtual Person Person { get; set; }
    public virtual User User { get; set; }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.UserId).GeneratedBy.Guid().ColumnName("UserId");
        Map(x => x.Username);
        Map(x => x.PasswordHash);
        Map(x => x.PasswordSalt);
        Map(x => x.Email);
        Map(x => x.IsApproved);
        Map(x => x.IsLockedOut);
    }
}

public class UserPersonalizationMap : ClassMap<UserPersonalization>
{
    public UserPersonalizationMap()
    {
        WithTable("UserPersonalization");
        Id(x => x.UserPersonalizationId).ColumnName("UserPersonalizationId");
        References(x => x.Person).ColumnName("PersonId");
        References(x => x.User).ColumnName("UserId");
    }
}

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.PersonId).ColumnName("PersonId");
        Map(x => x.Name);
        References(x => x.Company).ColumnName("CompanyId");
    }
}
+5  A: 

Try this:

var ubercharged = session.CreateCriteria(typeof(Uber))
        .CreateCriteria("Foo")
            .Add(Restrictions.Eq("Name", "somename"))
            .UniqueResult<Uber>();
Stuart Childs
Thanks, it looks like the mappings don't work correctly. This went a bit further but it looks like the Id's aren't currently set for the foreign key elements...
kitsune
Had to specify the mappings more precisely... thanks for your guidance!
kitsune
+1  A: 

Can you sort using "ubercharged.AddOrder(Order.asc("Foo.Name")) syntax? This syntax should work in NHib 2.01. If not, your maps are not working correctly.

Stuart's answer should work fine for you though.

Chad Ruppert