0

This is related to this question I asked previously.

In Request mapping I have it set to SaveUpdate due and in Discount mapping I have cascade set to none.

There are two scenarios:

  1. The first is new request and new discount. Created both then added the discount to the request and saved the request; this works as expected.

  2. Next scenario is new request and existing discount. This is not working right and I'm unsure why. Below is the SQL statements ran in test (omit values):

    NHibernate: INSERT INTO Requests
    NHibernate: SELECT @@identity
    NHibernate: INSERT INTO DiscountRequests (DiscountId, RequestId) VALUES (3, 5);
    NHibernate: UPDATE Discounts
    NHibernate: DELETE FROM DiscountRequests WHERE DiscountId = 3;
    

The final line is the issue: it is going back and deleting the insert from line 3. The command I am applying to the Request object is session.Save(request);, nothing else.

Would there be any reason why the call to Delete is made?

Edit:

Update Code

 public void Add(Request request)
    {

        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(request);
                transaction.Commit();
            }
        }
    }

Discount Mapping

<join table="DiscountRequests" optional="true">
  <key column="DiscountId" />
  <many-to-one name="Request" column="RequestId" cascade="none" />
</join>

Request Mapping

<join table="DiscountRequests" optional="true">
    <key column="RequestId" />
    <many-to-one name="Discount" column="DiscountId" cascade="save-update" />
</join>

ttp://stackoverflow.com/questions/14837373/zero-to-one-relationship-nhibernate-mapping

Chris Crew
  • 353
  • 4
  • 15
  • 1
    Have you updated both sides of the relationship? Are you using the trick with the element from the other question? Can you show the code that performs the changes? – Oskar Berggren Feb 17 '13 at 11:28
  • Depending on your `Cascade` settings, updating `Discounts` might be causing the delete. Do you have a cascade attribute in the `DiscountsRequests` property of the `Discounts` class, set to `save, update`? Can you post the mapping files for all three tables? – rae1 Feb 19 '13 at 05:15

1 Answers1

0

I disagree with the accepted answer to your earlier question; see this article for an explanation of join mapping.

I would model this as a simple many-to-many relationship (or two one-to-manys if DiscountRequest has additional data). You can map the collections as fields and enforce the rule that a Discount has 0 or 1 Request (and vice versa) through a public property. If it's a many-to-many then the cascade setting should be none.

For example:

public class Request
{
    private ICollection<Discount> _discounts;

    public Request() { _discounts = new Hashset<Discount>(); }

    public Discount
    {
        get { return _discounts.SingleOrDefault(); }
        set
        {
            _discounts.Clear();
            if (value != null) { _discounts.Add(value);
        }
     }
}
Community
  • 1
  • 1
Jamie Ide
  • 48,427
  • 16
  • 81
  • 117