Nhibernate with list of objects changing objects linked by FK

Asked

Viewed 23 times

0

A Document has a Items list mapped with Cascade all (when saving the document, items must be saved).
Each item has a reference to the FK of the previously registered product.
When opening the Session and loading the document with the items (Lazy load), I open another Session and change the product value of an item from 1.50 to 1.90 for example, write using Session.Merge and close the product Session.
Problem, when saving the document it re-writes the product with the old price.
In debug I’ve seen that the price of the product within the list of items in the document has been updated, but in the sql generated by nhibernate I saw that he insists on generating with the old price (I don’t know where he’s picking up)

Is there any way for me to map to the Document to persist only the items? without persisting product changes?

Mapping of document x items:

.Override<Documento>(map => map.HasMany(x => x.Itens).Cascade.All().OrderBy("NumeroItem"))

I have the following Repository standard for all CRUD operations (I will post here only the modification method):

public void Alterar(T entidade)
{
    using (ITransaction transacao = Session.BeginTransaction())
    {
        try
        {
            Session.Merge(entidade);
            transacao.Commit();
            Session.Flush();
            SetExportacaoCadastroDesatualizada(entidade);
        }
        catch (Exception ex)
        {
            if (!transacao.WasCommitted)
            {
                transacao.Rollback();
            }
            Console.WriteLine("Erro ao gravar:\n" + ex.Message + "\n" + ex.InnerException);
            throw ex;
        }
    }
}

Document class:

public class Documento 
{
    public virtual int Id { get; set; }
    public virtual int DocumentoNumero { get; set; }
    public virtual IList<DocumentoItem> Itens { get; set; }
}

Class Documentitem:

public class DocumentoItem 
{
    public virtual int Id { get; set; }
    public virtual int NumeroItem { get; set; }
    public virtual ProdutoBarra ProdutoBarra { get; set; }
  
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.