Reputation: 14897
I have two classes, Vendor and contact. Vendor is a parent of Contact. There's a one-to-many relationship between Vendor and Contact. I have set the cascading relationship between Vendor and Company as none. This means (at least to me) when I change a Contact for a Vendor and save the Vendor, this saving action should NOT cascade to the Contact and therefore, any changes I make to Contact should not persist to the database.
However, this is not the case. When I change the FirstName of a Contact and save its parent, the saving action is cascaded to Contact. For example using below's test function, I see a Contact with the FirstName = "This_new_first_name_should_not_be_saved". Initially, I did not expect to see this change persist to the database.
Am I simply misunderstanding how cascade works or is there something wrong with my code?
Test.cs
[Test]
public void RemoveManyToOneAssociation()
{
var container = BuildContainer();
var vendorRepo = container.Resolve<Repository<Vendor>>(); //Get respository
var contactRepo = container.Resolve<Repository<Contact>>(); //Get repository
var vendor = vendorRepo.FirstOrDefault();
var contact = vendor.Contacts.FirstOrDefault();
contact.FirstName = "This_new_first_name_should_not_be_saved";
vendor.Contacts.Remove(contact);
vendorRepo.Save(vendor);
//The saving action above should not cascade to contacts because we have set cascade to "none"
}
Vendor.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DataAccess"
namespace="DataAccess">
<class name="Vendor">
<id name="Id" type="int" unsaved-value="0">
<generator class="hilo" />
</id>
<property name="VendorName" />
<set name="Contacts" inverse="true" cascade="none">
<key column="VendorID" />
<one-to-many class="Contact"/>
</set>
<many-to-one name="Company" column="CompanyID"></many-to-one>
</class>
</hibernate-mapping>
Contact.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DataAccess"
namespace="DataAccess">
<class name="Contact">
<id name="Id" type="int">
<generator class="hilo" />
</id>
<property name="FirstName" />
<property name="LastName" />
<many-to-one name="Vendor" class="Vendor" column="VendorID"></many-to-one>
</class>
</hibernate-mapping>
Vendor.cs
public class Vendor : Entity<int>
{
public Vendor()
{
}
public Vendor(string name)
{
VendorName = name;
}
public virtual string VendorName { set; get; }
public virtual ISet<Contact> Contacts { set; get; }
public virtual Company Company { set; get; }
public virtual void CopyTo(Vendor target)
{
target.VendorName = VendorName;
target.Company = Company;
}
}
Contact.cs
public class Contact : Entity<int>
{
public Contact()
{
//it's not the best practice to initialize virtual properties in constructor but we're controlling
//the inheritance in this so doing this should be fine
// http://stackoverflow.com/a/469577/89605
FirstName = "";
LastName = "";
}
public Contact(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public virtual string FirstName { set; get; }
public virtual string LastName { set; get; }
public virtual Vendor Vendor { set; get; }
public virtual void CopyTo(Contact contact)
{
contact.FirstName = FirstName;
contact.LastName = LastName;
}
}
Update *Repository.cs*
public class Repository<T> where T : class
{
private ISession _session;
public Repository(ISession session)
{
_session = session;
}
public virtual T GetById(int id)
{
return _session.Get<T>(id);
}
public virtual T LoadById(int id)
{
return _session.Load<T>(id);
}
public virtual List<T> GetAll()
{
return _session.Query<T>().ToList();
}
public virtual T FirstOrDefault()
{
return _session.Query<T>().FirstOrDefault();
}
public virtual void Save(T entity)
{
using (ITransaction _transaction = _session.BeginTransaction())
{
_session.Save(entity);
_transaction.Commit();
}
}
public virtual void Delete(T entity)
{
using (ITransaction _transaction = _session.BeginTransaction())
{
_session.Delete(entity);
_transaction.Commit();
}
}
}
Upvotes: 1
Views: 2463
Reputation: 52725
As you have already inferred, you are misunderstanding how cascading works. More precisely, how NHibernate works.
All changes you make to already-persistent entities are persisted on Flush
.
A call to session.Save()
on an entity retrieved using the same session is a NO-OP (I have to assume that's what your Repository<T>.Save()
method does, as I don't have your sources)
Upvotes: 4