EDIT: Sorry Wrong Forum, Moderators could you please move the thread to NHibernate?
Hi guys I just started with NHibernate and I'm really amazed
This is my problem, I have two classes
Code:
public class RegimentDb
{
public virtual Guid Id { get; set; }
public virtual int PosX { get; set; }
public virtual int PosY { get; set; }
}
and
Code:
public virtual RegimentDb Regiment { get; set; }
public virtual int UnitType { get; set; }
public virtual int Number { get; set; }
The logic behind is that every Regiment has Units - for example Regiment 1 has 500 Units of UnitType 3 and 200 Units of UnitType 6
My problem is that NHibernate requires a Unique ID, but currently RegimentUnit has no unique ID. But then I noticed that the combination Regiment(ID) and UnitType is unique so I tried to make
a Composite ID
Code:
public class RegimentMap : ClassMap<RegimentDb>
{
public RegimentMap()
{
Table(DB.Tbl.Regiments.Name);
Id(x => x.Id);
Map(x => x.PosX);
Map(x => x.PosY);
HasMany(x => x.Units)
.Inverse()
.Cascade.All();
}
}
public class RegimentUnitMap : ClassMap<RegimentUnitDb>
{
public RegimentUnitMap()
{
Table(DB.Tbl.RegimentUnits.Name);
CompositeId()
.KeyReference(x => x.Regiment)
.KeyProperty(x => x.UnitType);
Map(x => x.UnitType);
Map(x => x.Number);
//References(x => x.Regiment);
}
}
I hope that my understanding of Composite IDs is correct. Now I get the error:
composite-id class must override Equals()
I have read that a composite ID must have a own equals and hash method and the description of the method says you should avoid coposite IDs, why is that the case?
Is there maybe something wrong in my code or is there a better way to solve this problem (except creating a own ID for RegimentUnit) ?
Desert Fox