简体   繁体   English

Nhibernate映射父级>子级的最佳方法

[英]Nhibernate Best way to mapping Parent > Child

I'm new in NHibernate and I have some doubts. 我是NHibernate的新手,我对此有些怀疑。 I'll put my code bellow and then I'll explain. 我将我的代码放在下面,然后我将解释。

My tables: 我的桌子: https://dl.dropbox.com/u/423844/map%20db.png

My classes: 我的课程:

public class IntegratorModel : PGIBaseModel<int>, IIntegratorModel
{
    public virtual string Identifier { get; set; }

    public virtual string Name { get; set; }

    public virtual string Description { get; set; }
}

public class ClientModel : PGIBaseModel<int>, IClientModel
{
    public virtual string Name { get; set; }

    public virtual string TokenPGI { get; set; }

    public virtual IIntegratorModel Integrator { get; set; }
}

My mappings using Fluent: 我使用Fluent的映射:

public class IntegratorMappingOverride : IAutoMappingOverride<IntegratorModel>
{
    public virtual void Override(AutoMapping<IntegratorModel> mapping)
    {
        mapping.Map(model => model.Identifier).Column("Identifier").Length(4);
        mapping.Map(model => model.Name).Column("Name").Length(200);
        mapping.Map(model => model.Description).Column("Description").Length(2000);
    }
}

    public class ClientMappingOverride : IAutoMappingOverride<ClientModel>
{
    public virtual void Override(AutoMapping<ClientModel> mapping)
    {
        mapping.Map(model => model.Name).Column("Name").Length(200);
        mapping.Map(model => model.TokenPGI).Column("TokenPGI").Length(50);

        mapping.References<IntegratorModel>(model => model.Integrator);
    }
}

I'm saving IntegratorModel previously in another moment, so when I'm inserting a new ClientModel I already have the IntegratorID, so it's ok. 我之前又保存了IntegratorModel,所以当我插入新的ClientModel时,我已经具有IntegratorID,所以可以。

My question is when I'm updating ClientModel and I want to change IntegratorID in the database, I'm doing something like this: 我的问题是,当我更新ClientModel并想更改数据库中的IntegratorID时,我正在执行以下操作:

    IClientModel model = this.GetModelFromPost();
    model.Integrator = integratorBLL.LoadByID(context.Request.Form["IntegratorID"]));
    clientBLL.Save(model);

integratorBLL.LoadByID just will call ISession.Load from NHibernate. integratorBLL.LoadByID只会从NHibernate调用ISession.Load。 Maybe I'm wrong, but Load doesn't hit the database in this moment. 也许我错了,但是Load在这一刻没有打中数据库。

My question is if what I'm doing is correct. 我的问题是我在做什么是否正确。 In this case, won't be easier if I had some property IntegratorID directly in ClientModel e just updated it? 在这种情况下,如果我直接在ClientModel e中直接更新了一些属性IntegratorID,会不会更容易?

Cause, in my architeture, I have to call my BLL of Integrator, call Load and then set the return in ClientModel property. 因为,在我的体系结构中,我必须调用我的Integrator的BLL,调用Load,然后在ClientModel属性中设置返回值。

This code works, but cause I'm new at Nhibernate, I think that probably there's another way to do it better. 这段代码有效,但是由于我是Nhibernate的新手,所以我认为可能还有另一种方法可以使它更好。

session.Load() is meant for this scenario and has the advantage that the objectproxy you get behaves like the real object so you don't have a broken model when set only the id and you want to use the clientmodels integrator later on. session.Load()适用于这种情况,它的优点是您获得的objectproxy的行为类似于真实对象,因此当仅设置id且以后要使用clientmodels集成器时, 模型不会损坏 It also does not break change tracking since NHib can not seperate between null > not initialised and null > references nothing 它也不会中断更改跟踪,因为NHib无法在null > not initialisednull > references nothing之间进行分隔

To sum up: this is the right way to handle this with (N)Hibernate. 总结:这是使用(N)Hibernate处理此问题的正确方法。

The code is ok. 代码还可以。 You don't need to call Save after changing the entity. 更改实体后,您无需调用“保存”。 NHibernate finds the changes itself and propagates all changes to the database when committing the session. 提交会话时,NHibernate会自行查找更改并将所有更改传播到数据库。

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

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