且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CompositeId原因无法编译映射文档错误

更新时间:2022-10-16 09:38:13

编译映射文件:(XmlDocument)?我的理论是composite-id类必须重写Equals():YOURNAMESPACE.PriorityListPart。

对于需要composite-id的实体,对象本身被用作钥匙。为了使相同的对象被识别,你需要重写Equals和GetHashCode方法。

您实体的Equals方法的示例如下所示:

  public override bool Equals(object obj)
{
var other = obj as PriorityListPart;

if(ReferenceEquals(null,other))return false;
if(ReferenceEquals(this,other))返回true;

返回this.AssemblyPartNumber == other.AssemblyPartNumber&&
this.PartNumber == other.PartNumber;



$ b

你实体的一个GetHashCode方法就是这样的:



$ $ $ $




$ ().GetHashCode();
hash =(hash * 31)^ AssemblyPartNumber.GetHashCode();
hash =(hash * 31)^ PartNumber.GetHashCode();

返回散列;






$ b

这也意味着如果你想检索一个对象,你不能有一个单一的关键。要使用复合键组件正确检索特定对象,您使用的键实际上是对象的一个​​实例,将复合键组件设置为您希望检索的实体。

这就是Equals()方法必须被覆盖的原因,所以NHibernate能够根据你指定的内容来确定你实际上试图检索哪个对象Equals方法。

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.