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:
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.
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
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: