使用Nhibernate.Mapping.Attributes指定组合键

时间:2013-11-07 06:49:54

标签: c# .net hibernate nhibernate nhibernate-mapping

如何使用Nhibernate Mapping Attributes为实体指定复合键。

以下是具有复合键的示例实体。这里的关键是: InternalCompanyCode CmAddress

目前我正在使用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; }

...

}

2 个答案:

答案 0 :(得分:2)

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

此论坛讨论了解决方案: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

的评论

答案 1 :(得分:0)

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