This group is for discussion re: the development of the NHibernate project
itself.  For discussion re: development of your own projects _with_
NHibernate, please direct your postings to the NHUSERS group at
http://groups.google.com/group/nhusers.

Steve Bohlen
sboh...@gmail.com
http://blog.unhandled-exceptions.com
http://twitter.com/sbohlen

On Fri, May 22, 2015 at 8:50 AM, Alessandro Colla <eglat...@gmail.com>
wrote:

> Hi,
> I'm struggling witha problem related to paretn-child mapping.
> I think I did everything correctly, but every time I add a child and save
> the parent, nhibernate execute an insert and then an update for all the
> rows in the child list.
> here is my classes and mapping
>
>   public class Cliente : EntityBase
>   {
>     public virtual string Cognome { get; set; }
>     public virtual string Nome { get; set; }
>     public virtual int AttivitaId { get; set; }
>     public virtual DateTime? DittaAssunzioneData { get; set; }
>     public virtual decimal Stipendio { get; set; }
>
>     public virtual IList<ClienteChiusura> Chiusure { get; protected set; }
>
>     public virtual void AddTelefonata(Telefonata telefonata)
>     {
>       if (Telefonate == null)
>         Telefonate = new List<Telefonata>();
>       //NHibernate impedence
>       telefonata.Cliente = this;
>       Telefonate.Add(telefonata);
>     }
>
>     public virtual void RemoveTelefonata(int telefonataId)
>     {
>       if (Telefonate == null)
>         return;
>       var telefonata = Telefonate.SingleOrDefault(x => x.Id == telefonataId);
>       if (telefonata != null)
>       {
>         Telefonate.Remove(telefonata);
>         telefonata.Cliente = null;
>       }
>     }
>   }
>
>   public class Telefonata : EntityBase
>   {
>     public virtual Cliente Cliente { get; set; }
>     public virtual string Descrizione { get; set; }
>     public virtual DateTime? Data { get; set; }
>     public virtual bool Ingresso { get; set; }
>     public virtual string Utente { get; set; }
>     public virtual DateTime? UtenteData { get; set; }
>     public virtual TelefonataTipo TelefonataTipo { get; set; }
>   }
>
>   public class TelefonataTipo : EntityBase
>   {
>     public virtual string Nome { get; set; }
>   }
>
>
>   public abstract class EntityBase
>   {
>    public virtual int Id { get; private set; }
>
>    //ctor
>    protected EntityBase()
>    { }
>
>    public override int GetHashCode()
>    {
>      return Id.GetHashCode();
>    }
>
>    public override bool Equals(object obj)
>    {
>      return (obj != null && obj.GetType() == GetType() && 
> ((EntityBase)obj).Id == Id);
>    }
>
>    public static bool operator ==(EntityBase entity1, EntityBase entity2)
>    {
>      //cast come object altrimenti ho un loop ricorsivo
>      if ((object)entity1 == null && (object)entity2 == null)
>        return true;
>
>      if ((object)entity1 == null || (object)entity2 == null)
>        return false;
>
>      if (entity1.GetType() != entity2.GetType())
>        return false;
>
>      if (entity1.Id != entity2.Id)
>        return false;
>
>      return true;
>    }
>
>    public static bool operator !=(EntityBase entity1, EntityBase entity2)
>    {
>      return (!(entity1 == entity2));
>    }
> }
>
>
>
> And the mapping is as follow
>
> public class ClienteMapping : 
> MappingBase<Evoltel.PuntoQuinto.Domain.Entities.Cliente.Cliente>
>   {
>     public ClienteMapping()
>       : base("CLIENTI")
>     {
>       Id(x => x.Id, c => { c.Column("CLIENTE_ID"); 
> c.Generator(Generators.Native, g => g.Params(new { sequence = 
> "GEN_CLIENTI_ID" })); });
>       Property(x => x.Cognome, c => c.Column("COGNOME"));
>       Property(x => x.Nome, c => c.Column("NOME"));
>       Property(x => x.AttivitaId, c => c.Column("ATTIVITA_ID"));
>       Property(x => x.DittaAssunzioneData, c => 
> c.Column("D_ASSUNZIONE_DATA"));
>       Property(x => x.Stipendio, c => c.Column("D_STIPENDIO"));
>
>       Bag(x => x.Telefonate, map =>
>       {
>         map.Inverse(true);
>         map.Table("TELEFONATE");
>         map.Cascade(Cascade.All | Cascade.DeleteOrphans);
>         map.Key(k => k.Column("CLIENTE_ID"));
>         map.Lazy(CollectionLazy.NoLazy);
>       }, r => r.OneToMany());
>   }
>
> public class TelefonataMapping : 
> MappingBase<Evoltel.PuntoQuinto.Domain.Entities.Cliente.Telefonata>
> {
>   public TelefonataMapping()
>     : base("TELEFONATE")
>   {
>     Id(x => x.Id, c => { c.Column("TELEFONATA_ID"); 
> c.Generator(Generators.Native, g => g.Params(new { sequence = 
> "GEN_TELEFONATE_ID" })); });
>     ManyToOne(x => x.Cliente, c => c.Column("CLIENTE_ID"));
>     Property(x => x.Descrizione, c => c.Column("DESC_LUNGA"));
>     Property(x => x.Data, c => c.Column("TELEFONATA_DATA"));
>     Property(x => x.Ingresso, c => { c.Column("INGRESSO"); 
> c.Type<TrueFalseType>(); });
>     Property(x => x.Utente, c => c.Column("UTENTE_NOME"));
>     Property(x => x.UtenteData, c => c.Column("UTENTE_DATA"));
>     ManyToOne(x => x.TelefonataTipo, c => { c.Column("TELEFONATA_TIPO_ID"); 
> c.NotFound(NotFoundMode.Ignore); });
>   }
> }
>
> public abstract class MappingBase<T> : ClassMapping<T> where T : EntityBase
>  {
>    protected MappingBase(string tableName)
>    {
>      Table(tableName);
>      Lazy(false);
>      DynamicUpdate(true);
>    }
>  }
>
>
>
> If I do not use Cascade.All the new Child is not saved (and for what I
> understood of NHibernate it's correct)
>
> What is that I'm not seeing?
>
>  --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nhibernate-development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nhibernate-development+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"nhibernate-development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nhibernate-development+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to