我试图弄清楚这个问题已经很长时间了。我有一个 hacky 方法让它工作。
我只想知道这在 Fluent NHibernate 映射中是否可行。
假设我有两个表,例如:
Table ComissionLevel
{
Year,
ComissionLevelID,
... other properties ....
}
primary key (Year,ComissionLevelID)
Table ClientCommission
{
Year,
ClientID,
CommissionLevelID_1,
CommissionLevelID_2,
... other properties ...
}
primary key (Year,ClientID)
foreign key CommissionLevel1 (Year,CommissionLevelID_1)
foreign key CommissionLevel2 (Year,CommissionLevelID_2)
目前我的映射如下:
public ComissionLevelMap()
{
Schema("XXXX");
Table("ComissionLevel");
LazyLoad();
CompositeId()
.KeyProperty(x => x.Year, set => {
set.ColumnName("Year");
set.Access.Property(); } )
.KeyProperty(x => x.CommissionLevelID, set => {
set.ColumnName("CommissionLevelID");
set.Length(10);
set.Access.Property(); } );
HasMany<ClientCommission>(x => x.ClientCommissions)
.Access.Property()
.AsSet()
.Cascade.AllDeleteOrphan()
.LazyLoad()
.Inverse()
.Generic()
.KeyColumns.Add("Year", mapping => mapping.Name("Year")
.SqlType("NUMBER")
.Nullable())
.KeyColumns.Add("CommissionLevelID_1", mapping => mapping.Name("CommissionLevelID_1")
.SqlType("VARCHAR2")
.Nullable()
.Length(10));
HasMany<ClientCommission>(x => x.ClientCommission2s)
.Access.Property()
.AsSet()
.Cascade.AllDeleteOrphan()
.LazyLoad()
.Inverse()
.Generic()
.KeyColumns.Add("Year", mapping => mapping.Name("Year")
.SqlType("NUMBER")
.Nullable())
.KeyColumns.Add("CommissionLevelID_2", mapping => mapping.Name("CommissionLevelID_2")
.SqlType("VARCHAR2")
.Nullable()
.Length(10));
}
public ClientCommissionMap()
{
Schema("XXXXX");
Table("ClientCommission");
LazyLoad();
CompositeId()
.KeyProperty(x => x.ClientID, set => {
set.ColumnName("ClientID");
set.Length(10);
set.Access.Property(); } )
.KeyProperty(x => x.Year, set => {
set.ColumnName("Year");
set.Access.Property(); } );
References(x => x.ComissionLevel1)
.Class<ComissionLevel>()
.Access.Property()
.Cascade.None()
.LazyLoad()
.Insert()
.Update()
.Columns("Year", "CommissionLevelID_1");
References(x => x.ComissionLevel2)
.Class<ComissionLevel>()
.Access.Property()
.Cascade.None()
.LazyLoad()
.Insert()
.Update()
.Columns("Year", "CommissionLevelID_2");
}
我现在的问题是,每当我创建一个 CommissionLevel 并将 ClientCommission 分配给它的集合时,如果我通过调用 session.save(CommissionLevel) 保存它们,它会抛出一个异常
<Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index>.
我的问题是:
NHibernate 会自动保存关系吗?像:
ClientCommission commission = new ClientCommission{Year = 2012, ClientID =SomeGuid}; CommissionLevel newCommissionLevel = new CommissionLevel{Year = 2012, CommissionLevelID =NewCommissionLevelGuid}; newCommissionLevel.ClientCommission1s.Add(commission); newCommissionLevel.ClientCommission2s.Add(commission); CommissionLevelRepo.Save(newCommissionLevel);
当我调用 CommissionLevelRepo.Save(newCommissionLevel) 时,NHibernate 是否也会更新 ClientCommission.ComissionLevel1 和 ClientCommission.ComissionLevel2
还是我必须说
ClientCommission.ComissionLevel1 = newCommissionLevel;
ClientCommission.ComissionLevel2 = newCommissionLevel;
- 对于我得到的异常,这是因为 NHibernate 没有生成正确的列,它似乎会生成三个 Year 列。因为如果我手动创建两个名为 ComissionLevelID1 和 CommissionLevelID2 的属性,禁用 ClientCommission 上的 .Insert() 和 .Update() 它将正确保存它。
有人可以告诉我映射这两个类的正确方法吗?
非常感谢。