这里的行为很糟糕。如果我创建一个新的HashCode,保存HashCode,然后将事务添加到Transactions集合,级联在Update上失败。 Transaction对象没有出现在DB中,奇怪的是,HashCode对象的属性也没有更新!
我不知道是什么原因引起的。这是相关的映射:
<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);
}
任何人都有任何建议或想法吗?
答案 0 :(得分:5)
你不是在任何地方冲洗会议。
答案 1 :(得分:0)
将更新方法更改为以下内容:
public virtual void Update(TObj obj)
{
using (ITransaction tx = CurrentSession.BeginTransaction())
{
CurrentSession.SaveOrUpdate(obj);
tx.Commit();
}
}