Problem saving

1

Only one form that is returning this error, the question I do not know what can be or how to do it, some ask to put the relationship in cascade what is already.

  NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: BlogWeb.Models.Veiculo, Entity: BlogWeb.Models.Veiculo

Error return line:

public void Adiciona(Abastecimento abastecimento)
    {
        ITransaction tx = session.BeginTransaction();
        session.Save(abastecimento);
        tx.Commit();
    }

Vehicle Mapping:

public class VeiculoMapping : ClassMap<Veiculo>
{
    public VeiculoMapping()
    {
    Id(p => p.Id).GeneratedBy.Identity();
    Map(p => p.NCarro);
    Map(p => p.Modelo);
    Map(p => p.Ano);

    }
}

Vehicle Model:

public class Veiculo
{
    public virtual int Id { get; set; }
    [Required]
    public virtual int NCarro { get; set; }
    [Required]
    public virtual string Modelo { get; set; }
    public virtual int Ano { get; set; }
}

Mapping Supply:

public class AbastecimentoMapping : ClassMap <Abastecimento>
    {
        public AbastecimentoMapping()
        {
            Id(a => a.Id).GeneratedBy.Identity();
            Map(a => a.DtAbastecido);
            Map(a => a.Litro);
            Map(a => a.VlrUnit);
            Map(a => a.Km);
            Map(a => a.TotalGasto);
            Map(a => a.Km_Andado);
            References(a => a.NomeProduto, "NomeProdutoId");
            References(a => a.Autor, "AutorId");
            References(a => a.NumCarro, "NumCarroId");
        }
    }

Supply Model:

public class Abastecimento
{
    public virtual int Id { get; set;}
    [Required]
    public virtual int Litro { get; set; }
    public virtual DateTime? DtAbastecido { get; set; }
    public virtual decimal VlrUnit { get; set; }
    public virtual int Km { get; set; }
    public virtual decimal TotalGasto { get; set; }
    public virtual int Km_Andado { get; set; }
    public virtual Usuario Autor { get; set; }
    public virtual Compra NomeProduto { get; set; }
    public virtual Veiculo NumCarro { get; set; }
}
    
asked by anonymous 28.02.2018 / 18:55

1 answer

1

You are saving an entity, which has a relationship with some entity that has been changed but has not been saved before.

If this is the case, you have two options:

  • Save the other entity before saving this;

  • Map the relationship as cascade , so that in this situation the other related entity is saved as well.

Speaking in cascade

For each basic NHibernate session operation - including Persist (), Merge (), SaveOrUpdate (), Delete (), Lock (), Refresh (), Evict (), Replicate () - there is a corresponding cascade style. Respectively, cascading styles are called persisting, merge, save-update, delete, lock, refresh, eject, replicate. The cascading style for Save () and Update () is save-update; for SaveAndUpdateCopy () is merge; and for PersistOnFlush () is persistent. And remove is an alias to delete.

If you want an operation to be cascaded over an association, you must indicate this in the mapping document. For example:

<one-to-one name="person" cascade="persist"/>

My combo cascading styles:

<one-to-one name="person" cascade="persist,delete,lock"/>

You can use cascade = "all" to specify that all operations must be cascaded throughout the join. The cascade padrão = "none" specifies that no operation should be cascaded.

I recommend reading more on this post in English HERE

    
01.03.2018 / 18:12