1

我有两个类,供应商和联系人。供应商是联系人的父级。供应商和联系人之间存在一对多的关系。我已将供应商和公司之间的级联关系设置为无。这意味着(至少对我而言)当我更改供应商的联系人并保存供应商时,此保存操作不应级联到联系人,因此,我对联系人所做的任何更改都不应保留到数据库中。

然而,事实并非如此。当我更改联系人的名字并保存其父级时,保存操作将级联到联系人。例如,使用下面的测试函数,我看到一个名字为“This_new_first_name_should_not_be_saved”的联系人。最初,我没想到会看到此更改持续到数据库中。

我只是误解了级联的工作原理还是我的代码有问题?

测试.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"

        }

供应商.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>

联系人.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>

供应商.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;
        }
    }

联系人.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;
        }
    }

更新 * 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();
            }
        }
    }
4

1 回答 1

4

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)

于 2013-03-19T16:43:11.500 回答