简体   繁体   English

流利的NHibernate hasmany保存插入空值

[英]Fluent NHibernate hasmany save insert null value

i'm new to nhibernate so maybe the response depends on my lack of knowledge. 我刚进入休眠状态,所以响应可能取决于我缺乏知识。

I created these two tables: 我创建了这两个表: 替代文字

(sorry for italian language, i hope you can understand withouth any problems). (对不起,意大利语,我希望您能毫无问题地理解)。

Then, i have these object in my model: 然后,我在模型中有这些对象:


[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; }
}

This is the way i mapped entities using fluent nhibernate: 这是我使用流畅的nhibernate映射实体的方式:


    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();

        }
    }

Now, i'd like to insert a new Profilo instance on my. 现在,我想在其上插入一个新的Profilo实例。 Everything seems to work but nhibernate does not insert values on TBA_Profilo.codice_sede column. 一切似乎正常,但是nhibernate不会在TBA_Profilo.codice_sede列上插入值。 I noticed that the insert statement is composed by two parameters (matricola, ruolo) - why does it forget the third parameter? 我注意到insert语句由两个参数(matricola,ruolo)组成-为什么它忘记了第三个参数?

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. 我在某个地方(在nhibernate邮件列表上)读了一个很普通的书,因为它首先将nhiberate插入值设置为null,然后使用list属性中包含的正确值更新相同的记录。

Is it right? 这样对吗? Am i doing any errors? 我有什么错误吗?

I hope to have clarified the situation. 我希望已经澄清了这种情况。 thx guys 谢谢你们

ps: I'm using Nhibernate 2.1 and fluent nhibernate 1.1 ps:我正在使用Nhibernate 2.1和流利的nhibernate 1.1

UPDATE : This is the code i use to save entity. 更新 :这是我用来保存实体的代码。


                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();
                        }
                    }
                }

UPDATE 2 : Following sly answer i slightly modified my solution. 更新2 :狡猾的回答后,我略有修改我的解决方案。 These are new model entities: 这些是新的模型实体:



[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; }
}

And new mappings: 以及新的映射:


 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();

        }
    }

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). 现在看来相当有效:我的意思是nhibernate可以写TBA_Profilo.codice_sede列,即使它不在Profilo.IList上显示(它会插入此List的最后一个元素)。

Any ideas? 有任何想法吗?

KeyColumns mapping is applied only child table in many-to-one connection. KeyColumns映射仅在多对一连接中应用子表。

If you want to have connection you will need to use Id column from TBA_portfolio table and reference it from TBA_Sede . 如果你想有连接,您将需要从使用ID列TBA_portfolio表和引用它TBA_Sede

Something like this: 像这样:

Tba_portfolio Id|matricola|ruolo Tba_portfolio ID | matricola | ruolo

Tba_sede Id|PorfolioId|descrizione Tba_sede ID | PorfolioId | descrizione

Your mapping is wrong, try this: 您的映射错误,请尝试以下操作:

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.SaveUpdate();

        }
    }

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")
            .Cascade.None()
            .Inverse();

    }

}

The key is, you need the parent profilio to save its child items. 关键是,您需要父配置文件来保存其子项。 Have the child items do nothing to its parent. 让子项对其父项不执行任何操作。 Also, your table diagram looks wrong, profolio should not have a key to sede, but sede should have a fk to profolio. 另外,您的表格看起来不对,profolio应该没有密钥要seed,但是sede应该有一个fk来保护profolio。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM