如何使用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; }
...
}
答案 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回答的补充,您还需要覆盖实体的Equals
和GetHashCode
成员。