I want to query with NHibernate Linq by component. The component contains a Date property which is not included in the override Equals. I just want to skip the Date property when asking for equality. At the moment Date property is set in the ctor of the Address class and the query will return 0 rows because of that. I know that this breaks the Value Object concept but I just want to know why the code below does not work properly.
THE QUERY As you can see the Date property is in the query
select user0_.Id as Id0_,
user0_.Name as Name0_,
user0_.Number as Number0_,
user0_.Date as Date0_
from test1 user0_
where (user0_.Number = 1 /* @p0 */
and user0_.Date = '2013-01-28T14:29:47.00' /* @p1 */)
MAIN
class Program
{
private static ISessionFactory _sessionFactory;
private static void CreateSessionFactory()
{
FluentConfiguration config = Fluently
.Configure(new Configuration().Configure())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMap>());
new SchemaExport(config.BuildConfiguration()).Create(false, true);
_sessionFactory = config.BuildSessionFactory();
}
[STAThread]
static void Main(string[] args)
{
CreateSessionFactory();
using (var session = _sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
var user = new User()
{
Name = "Nik",
Address = new Address(1)
};
session.Save(user);
tx.Commit();
}
using (var session = _sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
session.Query<User>().Where(x => x.Address == new Address(1)).Single();
tx.Commit();
}
}
}
CLASSES AND MAPPINGS
public class Address : IEquatable<Address>, IEqualityComparer<Address>
{
protected Address() { }
public Address(int number)
{
Number = number;
Date = DateTime.Now;
}
public virtual int Number { get; protected set; }
public virtual DateTime Date { get; protected set; }
public override bool Equals(object obj)
{
return Number == ((Address)obj).Number;
}
public override int GetHashCode()
{
return Number.GetHashCode();
}
public bool Equals(Address other)
{
return Number == other.Number;
}
public bool Equals(Address x, Address y)
{
return x.Number == y.Number;
}
public int GetHashCode(Address obj)
{
return obj.GetHashCode();
}
}
public class User
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual Address Address { get; set; }
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("test1");
Id(x => x.Id).GeneratedBy.GuidNative();
Map(x => x.Name);
Component(x => x.Address, cm =>
{
cm.Map(x => x.Number);
cm.Map(x => x.Date);
});
}
}
You have mapped Address as a component. Therefore, when you ask NHibernate to compare user.Address to some Address instance, it will compare every attribute. NHibernate doesn’t analyse compiled code to determine which of the properties are used in the Equals() method.
The code works properly.
You can write the query to compare every attribute separately instead of comparing the entire addresses. Or try to rethink the design to make Address a true value object.