I am writing an application which uses NHibernate mapping by code. When I have a TimeSpan mapped as a property of an entity, the TimeSpan is mapped correctly.
However, if I put the TimeSpan within a component mapping (value object), I get an en exception "Operand type clash: bigint is incompatible with time" - even though I am making sure I map the TimeSpan using the TimeSpan mapping.
The following code will save the timespan correctly (e.g. '1753-01-01T08:00:00')
public class MyEntity
{
public virtual int Id { get; protected set; }
public virtual ISet<AnotherEntity> OtherEntities { get; protected set; }
}
public class AnotherEntity
{
public virtual int Id { get; protected set; }
public virtual TimeSpan MyTimeSpan { get; protected set; }
}
public class MyEntityMap : ClassMapping<MyEntity>
{
public MyEntityMap()
{
Table("MyEntityTable");
Id(x => x.Id, map =>
{
map.Column("Id");
});
Set(x => x.OtherEntities, c =>
{
c.Table("OtherEntityTable");
c.Inverse(true);
c.Key(k => k.Column("MyEntityId"));
}, r => r.OneToMany(m =>
{
m.NotFound(NotFoundMode.Exception);
m.Class(typeof(AnotherEntity));
}));
}
}
public class AnotherEntityMap : ClassMapping<AnotherEntity>
{
public AnotherEntityMap()
{
Table("AnotherEntityTable");
Id(x => x.Id, map =>
{
map.Column("AnotherEntityId");
});
Property(x => x.MyTimeSpan, m => m.Type<TimeAsTimeSpanType>());
}
}
However, the following code will try to save the TimeSpan as an int (e.g. 288000000000) and throw an exception. (Similarly, an exception is thrown when the it tries to hydrate the nested value object.)
public class MyEntity
{
public virtual int Id { get; protected set; }
public virtual MyValueObject MyValueObject { get; protected set; }
}
public class MyValueObject
{
public virtual ISet<NestedValueObject> NestedValueObjects { get; protected set; }
}
public class NestedValueObject
{
public virtual TimeSpan MyTimeSpan { get; protected set; }
}
public class MyEntityMap : ClassMapping<MyEntity>
{
public MyEntityMap()
{
Table("MyEntityTable");
Id(x => x.Id, map =>
{
map.Column("Id");
});
Component(x => x.MyValueObject, c =>
{
c.Set(x => x.NestedValueObjects, map =>
{
map.Table("NestedValueObjects");
map.Key(k => k.Column("MyEntityId"));
}, r => r.Component(n =>
{
n.Property(x => x.MyTimeSpan, m => m.Type<TimeAsTimeSpanType>());
}));
});
}
}
Is there something incorrect with the mappings? Or is there a bug with NHibernate mapping by code?
I'm assuming this is a bug with NHibernate mapping by code, and have raised a bug NH-4038.