2

如何使用 Nhibernate 映射属性为实体指定复合键。

下面是一个带有复合键的示例实体。这里的键是:InternalCompanyCodeCmAddress

目前我正在使用 RawXml 属性来指定复合键。但我想使用 Id 或 CompositeId 或任何其他合适的属性来指定它。

[Class(Table = "APVENDP")]
public class APVendor 
{
    [RawXml(Content = "<composite-id><key-property name=\"CmAddress\" type=\"int\" column=\"B5AOCD\" /><key-property name=\"InternalCompanyCode\" type=\"int\" column=\"B5COCD\" /></composite-id>")]
    public int InternalCompanyCode { get; set; }

    public int CmAddress { get; set; }

    [Property(Name="ContactNumber", Column = "B5AMCD", Precision = 3, Scale=0)]
    public int ContactNumber { get; set; }

    [Property(Name="CurrencyCode", Column = "B5B3CD", Length = 4)]
    public String CurrencyCode { get; set; }

...

}
4

2 回答 2

3

“将所有键放在顶部而不是装饰字段”

该论坛讨论了解决方案:https ://forum.hibernate.org/viewtopic.php?p=2376310

例子:

[Class(Table = "APVENDP")]
public class APVendor
{
    [CompositeId(1)]
    [KeyProperty(2, Name = "CmAddress", Column = "B5AOCD")]
    [KeyProperty(3, Name = "InternalCompanyCode", Column = "B5COCD")]
    [Column(Name = "B5COCD")]
    private int? internalCompanyCode = 0;

    [Column(Name = "B5AOCD")]
    private int? cmAddress = 0;

    [Property(Name = "ContactNumber", Column = "B5AMCD", Precision = 3, Scale = 0)]
    [Column(Name = "B5AMCD")]
    private int? contactNumber = 0;

    [Property(Name = "CurrencyCode", Column = "B5B3CD", Length = 4)]
    [Column(Name = "B5B3CD")]
    private String currencyCode = "";

    ...
}

答案的功劳归功于jbl的评论

于 2013-12-05T08:01:22.557 回答
0

作为 Robin Rizvi 答案的补充,您还需要覆盖实体EqualsGetHashCode成员。

于 2018-05-23T10:51:02.150 回答