这里的Currious行为。如果我创建一个新的HashCode,保存HashCode,然后将事务添加到Transactions集合,级联在更新上失败。 Transaction对象不会出现在数据库中,并且,奇怪的是,HashCode对象的属性也不会更新!NHibernate的集合:更新不会级联
我不知道可能是什么原因造成的。下面是相关映射:
<class name="MyProject.HashCode, MyProject" table="HashCodes">
<id column="Id" name="Id">
<generator class="native" />
</id>
<many-to-one name="User" column="UserId" class="MyProject.User, MyProject" />
<property name="Hash" />
<property name="PasswordHash" />
<property name="InitialValue" update="false" />
<property name="CurrentValue" update="true" />
<property name="ClaimedDate" />
<property name="ClaimId" column="RowGuid" generated="insert" />
<bag name="Transactions" table="Transactions" cascade="all" inverse="true">
<key column="HashCodeId" />
<many-to-many column="Id" class="MyProject.Transaction, MyProject" />
</bag>
</class>
<class name="MyProject.Transaction, MyProject" table="Transactions">
<id column="Id" name="Id">
<generator class="native" />
</id>
<many-to-one name="HashCode" column="HashCodeId" class="MyProject.HashCode, MyProject" />
<property name="AmountCharged" />
<property name="AmountBilled" />
<property name="PreviousBalance" />
<property name="Memo" />
<property name="TransactionDate" generated="insert" update="false" />
</class>
这里是失败的,太考验的情况下,如果它是相关的:
[Test]
public void TransactionsCascadeWhenUpdatingHashCodes()
{
var user = TestFactory.CreateUser();
var hashCode = TestFactory.CreateHashCode(user);
var transaction = new Transaction
{
AmountBilled = hashCode.CurrentValue,
AmountCharged = decimal.Subtract(hashCode.CurrentValue, decimal.Multiply(hashCode.CurrentValue, 0.03M)),
HashCode = hashCode,
Memo = "This is a test",
PreviousBalance = hashCode.CurrentValue,
TransactionDate = DateTime.Now
};
hashCode.Transactions.Add(transaction);
// Now try to save it.
var hashCodeRepository = new HashCodeRepository(Session);
hashCodeRepository.Update(hashCode);
// Now see if that transaction is good to go
Assert.IsTrue(hashCode.Transactions[0].Id > 0);
// See if that transaction got persisted.
var loadedTransaction = Session.Load<Transaction>(hashCode.Transactions[0].Id);
Assert.IsNotNull(loadedTransaction);
}
// The repository method that is called...
public virtual void Update(TObj obj)
{
CurrentSession.Update(obj);
}
任何人有任何建议或想法?
是什么hashCodeRepository.Update办?如果您使用的是工作单元模式,通常不需要这种方法......除非更新将实体附加到会话中。如果实体已连接,只需刷新会话即可。你在做那个吗? – Iain 2010-11-03 06:37:27
看起来您误解了session.update的用途 - 这是为了重新附加分离的实体,而不是传播更改。 – UpTheCreek 2010-11-03 10:36:27
你的最后一个断言也有问题,Load不会做你认为会的事。 Load为你创建一个代理对象,当你访问其中一个属性时,它只会进入数据库进行水合作用,然后它会抛出一个异常或水合物 - 它永远不会返回null。你需要使用Session.Get(...) –
Gareth
2010-11-03 14:27:47