开发者

NHibernate collections: Update doesn't cascade

Currious behavior here. If I create a new HashCode, save the HashCode, then add a Transaction to the Transactions collection, the cascade is failing on Update. The Transaction object doesn't appear in the DB, and, curiously, the HashCode object's properties aren't updated either!

I have no idea what could be causing this. Here is the relevant mapping:

<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 开发者_StackOverflow中文版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>

Here is the test case that is failing, too, in case it's relevant:

[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);
}

Anyone have any suggestions or ideas?


You are not flushing the session anywhere.


Change your update method to the following:

public virtual void Update(TObj obj)
{
    using (ITransaction tx = CurrentSession.BeginTransaction())
    {
        CurrentSession.SaveOrUpdate(obj);
        tx.Commit();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜