I am trying to use CompositeId to map to a legacy system. The source database has a composite primary key so I can't use the normal this.Id mapping.
Here is my attempt to map it:
public PriorityListPartMap()
{
this.Schema("EngSchedule");
this.Table("vPriorityListPart");
this.CompositeId().KeyProperty(x => x.AssemblyPartNumber).KeyProperty(x => x.PartNumber);
this.Map(x => x.CurrentDueDate);
this.Map(x => x.OrderLine);
this.Map(x => x.OrderNumber);
this.Map(x => x.PartDescription);
this.Map(x => x.ProductCode);
this.Map(x => x.Revision);
}
When I try to create the session factory this mapping causes the error: Could not compile the mapping document: (XmlDocument)
I tried removing the CompositeId mapping and replaced it with:
this.Id(x => x.AssemblyPartNumber).GeneratedBy.Assigned();
The error goes away with that mapping but I can't really use that since the AssemblyPartNumber is not unique.
Is there a different way to map to a table with a composite primary key?
Thanks,
Matthew MacFarland
What is the inner exception for "Could not compile the mapping document: (XmlDocument)"? My theory is it will be "composite-id class must override Equals(): YOURNAMESPACE.PriorityListPart".
For entities requiring composite-ids, the object itself is used as the key. In order for objects that are 'the same' to be recognized as so, you need to override the Equals and GetHashCode methods.
An example Equals method for your entity would be something like this:
public override bool Equals(object obj)
{
var other = obj as PriorityListPart;
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return this.AssemblyPartNumber == other.AssemblyPartNumber &&
this.PartNumber == other.PartNumber;
}
An example GetHashCode method for your entity would be something like this:
public override int GetHashCode()
{
unchecked
{
int hash = GetType().GetHashCode();
hash = (hash * 31) ^ AssemblyPartNumber.GetHashCode();
hash = (hash * 31) ^ PartNumber.GetHashCode();
return hash;
}
}
This also means that if you want to retrieve an object, you cannot have a single key to do it with. To properly retrieve a specific object with its composite key components, the key you use is actually an instance of the object with the composite key components set to the entity you wish to retrieve.
This is why the Equals() method must be overridden, so that NHibernate has the ability to determine which object you are actually trying to retrieve, based on what you specify in the Equals method.