Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate hasmany save insert null value
    primarykey
    data
    text
    <p>i'm new to nhibernate so maybe the response depends on my lack of knowledge.</p> <p>I created these two tables: <img src="https://i.stack.imgur.com/DA3kn.png" alt="alt text"></p> <p>(sorry for italian language, i hope you can understand withouth any problems).</p> <p>Then, i have these object in my model:</p> <pre><code> [Serializable] public class Profilo { public virtual int Id { get; set; } public virtual string Matricola { get; set; } public virtual string Ruolo { get; set; } public virtual IList ListaSedi { get; set; } public Profilo() { ListaSedi = new List(); } } [Serializable] public class Sede { public virtual string CodiceSede { get; set; } public virtual string DescrizioneSede { get; set; } public virtual Profilo Profilo { get; set; } } </code></pre> <p>This is the way i mapped entities using fluent nhibernate:</p> <pre><code> public class Map_Sede : FluentNHibernate.Mapping.ClassMap { public Map_Sede() { Table("TBA_Sede"); Id(x => x.CodiceSede).Column("codice_sede").GeneratedBy.Assigned(); Map(x => x.DescrizioneSede) .Column("descrizione"); References(prof => prof.Profilo) .Column("codice_sede"); } } public class Map_Profilo : FluentNHibernate.Mapping.ClassMap { public Map_Profilo() { Table("TBA_Profilo"); Id(x => x.Id).Column("id").GeneratedBy.Identity(); Map(x => x.Matricola) .Column("matricola"); Map(x => x.Ruolo) .Column("ruolo"); HasMany(x => x.ListaSedi) .AsBag() .KeyColumns.Add("codice_sede") .Not.LazyLoad() .Cascade.None(); } } </code></pre> <p>Now, i'd like to insert a new Profilo instance on my. Everything seems to work but nhibernate does not insert values on TBA_Profilo.codice_sede column. I noticed that the insert statement is composed by two parameters (matricola, ruolo) - why does it forget the third parameter?</p> <p>I read somewhere (on nhibernate mailing list) that's quite normal 'cause nhiberate insert values with null first and then update the same records with right values contained in the list property.</p> <p>Is it right? Am i doing any errors?</p> <p>I hope to have clarified the situation. thx guys</p> <p>ps: I'm using Nhibernate 2.1 and fluent nhibernate 1.1</p> <p><strong>UPDATE</strong>: This is the code i use to save entity.</p> <pre><code> var sesionFactory = NHibernateHelper.createSessionFactory(); using (NHibernate.ISession session = sesionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { try { session.SaveOrUpdate(entity); transaction.Commit(); session.Flush(); } catch (Exception) { transaction.Rollback(); throw; } finally { transaction.Dispose(); } } } </code></pre> <p><strong>UPDATE 2</strong>: Following sly answer i slightly modified my solution. These are new model entities:</p> <pre><code> [Serializable] public class Profilo { public virtual int Id { get; set; } public virtual string Matricola { get; set; } public virtual string Ruolo { get; set; } public virtual IList ListaSedi { get; set; } public Profilo() { ListaSedi = new List(); } } [Serializable] public class Sede { public virtual string CodiceSede { get; set; } public virtual string DescrizioneSede { get; set; } public virtual IList ProfiliAssociati { get; set; } } </code></pre> <p>And new mappings:</p> <pre><code> public class Map_Sede : FluentNHibernate.Mapping.ClassMap { public Map_Sede() { Table("TBA_Sede"); Id(x => x.CodiceSede).Column("codice_sede").GeneratedBy.Assigned(); Map(x => x.DescrizioneSede) .Column("descrizione"); HasMany(x => x.ProfiliAssociati) .AsBag() .KeyColumns.Add("codice_sede") .Cascade.All(); } } public class Map_Profilo : FluentNHibernate.Mapping.ClassMap { public Map_Profilo() { Table("TBA_Profilo"); Id(x => x.Id).Column("id").GeneratedBy.Identity(); Map(x => x.Matricola) .Column("matricola"); Map(x => x.Ruolo) .Column("ruolo"); HasMany(x => x.ListaSedi) .AsBag() .Inverse() .KeyColumns.Add("codice_sede") .Cascade.SaveUpdate(); } } </code></pre> <p>Now it seems quite to work: i mean that nhibernate can write TBA_Profilo.codice_sede column even if it doesn't cicle on Profilo.IList (it inserts the last element of this List).</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload