Domanda

Problem encountered

When I create a transient instance with a children collection, everything gets persisted.

Aside, if I update an instance of one of the children object, it doesn't get updated when I save the parent object.

I'm actually using cascade="all"

Problem reproduction

The problem occurs when I have loaded all of my Customer occurences, and I change an invoice, though I always use the same ISession.

var repository = new CustomerRepository(session);
var customers = repository.GetAll();
var customer = customers.Where(c => c.Name == "Stack Overflow").FirstOrDefault();           
customer.Invoices
    .Where(i => i.Number == "1234")
    .Approve(WindowsIdentity.GetCurrent().Name);    
repository.Save(customer);

Step by step debugging clearly shows the repository.Save() method being executed, and the changes won't show inte the underlying database.

An update directly against the database table is possible, so no contraint causing the update to fail on the database-side.

Herewith some code in case it might help.

Customer.hbm.xml

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   namespace="MyProject.Model" 
                   assembly="MyProject">
    <class name="Customer" table="AC_CUST" schema="AC">
      <id name="Id" column="AC_CUST_ID" type="Int32" unsaved-value="0">
        <generator class="sequence-identity">
          <param name="sequence">AC_CUST_ID_SEQ</param>
          <param name="schema">AC</param>
        </generator>
      </id>
      <property name="Name" column="AC_CUST_NAME" type="String" 
                not-null="true" />
      <property name="PhoneNumber" column="AC_CUST_PHNUM" type="Int64" 
                not-null="true" />
      <bag name="Invoices" table="ESO_RAPP_ACCES_INFO_DSQ" schema="AC" 
           fetch="join" lazy="true" inverse="true" cascade="all">
        <key column="AC_CUST_ID" foreign-key="AC_CUST_INV_FK" />
        <one-to-many class="Invoice" />
      </bag>
    </class>
</hibernate-mapping>

Customer

public class Customer {
    public Customer() { Invoices = new List<Invoice>(); }

    public virtual int Id { get; proected set; }
    public virtual IList<Invoice> Invoices { get; protected set; }
    public virtual string Name { get; set; }
    public virtual string PhoneNumber { get; set; }
}

Invoice.hbm.xml

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   namespace="MyProject.Model" 
                   assembly="MyProject">
    <class name="Invoice" table="AC_INV" schema="AC">
      <id name="Id" column="AC_INV_ID" type="Int32" unsaved-value="0">
        <generator class="sequence-identity">
          <param name="sequence">AC_INV_ID_SEQ</param>
          <param name="schema">AC</param>
        </generator>
      </id>
      <property name="Approved" column="AC_INV_APPRD" type="DateTime" 
                not-null="false" />
      <property name="Approver" column="AC_INV_APPRR" type="String" length="15" 
                not-null="false" />
      <property name="Number" column="AC_INV_NUMBR" type="String" length="15" 
                not-null="true" />
      <property name="Produced" column="AC_INV_PROD" type="DateTime" 
                not-null="false" />
      <many-to-one class="Customer" column="AC_CUST_ID" />
    </class>
</hibernate-mapping>

Invoice

public class Invoice {
    public Invoice() { 
        Items = new List<Item>(); 
        Produced = DateTime.Now;
    }

    public virtual DateTime? Approved { get; protected set; }
    public virtual string Approver { get; protected set; }
    public virtual Customer Customer { get; set; }
    public virtual int Id { get; proected set; }
    public virtual string Number { get; set; }
    public virtual DateTime? Produced { get; set; }

    public virtual void Approve(string approver) {
        Approved = DateTime.Now;
        Approver = approver;
    }

    public virtual void Reject() { Produced = null; }
}

CustomerRepository

public class CustomerRepository {
    public CustomerRepository(ISession session) { Session = session; }

    public ISession Session { get; protected set; }

    public Customer Save(Customer instance) {
        Session.SaveOrUpdate(instance);
        return Instance;
    }
}

Related articles

Any help appreciated.

È stato utile?

Soluzione

Don't forget to Flush() your session!

NHibernate keeps track of all the changes along a session lifecycle which should only live for a form (Desktop) or a page (Web).

Because of the session is aware of all the changes, it doesn't necessarily commit the changes to the underlying database. Instead, it keeps a record of all the changes into a dictionary, then when it is flushed by calling ISession.Flush(), you actually demand the session to commit the changes for good.

So solution shall be:

repository.Save(customer);
session.Flush();

Or you may as well code a Commit() method within your repository which would Flush() the session upon a call.

repository.Save(customer);
repository.Commit();

And your repository would look like:

// Assuming you stock your session in the `Session` property.
public void Commit() { Session.Flush(); }

That's all!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top