如何使用 NHIbernate 将未持久化的对象附加到从数据库加载的对象

发布于 2024-11-04 06:27:53 字数 728 浏览 0 评论 0原文

我有一个如下所示的实体:

public class Driver
{
    IVehicle Vehicle {get;set;}
}

以及实现 IVehicle 的几个不同类(汽车、自行车、火车...)

我如何选择我的驱动程序持有的 IVehicle 的特定实现,并在下次加载驱动程序时记住此选择来自数据库?

我使用 NHibernate 3.0 进行持久化,并使用 FluentNHibernate 进行映射。

编辑

好吧,我终于想通了。我需要按以下方式映射车辆并映射每个子类:

public void VehicleMap : ClassMap<IVehicle>
{
    public VehicleMap()
    {
        Id(v => v.Id); // Needed to add a property which will be used as Id
         DiscriminateSubClassesOnColumn("TYPE");
    } 
}

public void CareMap : SubclassMap<Car>
{
    public CarMap()
    {
         DiscriminatorValue("CAR");
    }
}

此外,我需要从驱动程序映射中禁用车辆的延迟加载

I have an entity which looks likes this:

public class Driver
{
    IVehicle Vehicle {get;set;}
}

and several different classes which implement IVehicle (Car, Bike, Train...)

How can I select which specific implementation of IVehicle my Driver holds, and remember this selection the next time I load the driver from the DB?

I'm using NHibernate 3.0 for persistence together with FluentNHibernate for mapping.

EDIT

Ok I figured it out finally. I needed to map the vehicle and to map each subclass in the following way:

public void VehicleMap : ClassMap<IVehicle>
{
    public VehicleMap()
    {
        Id(v => v.Id); // Needed to add a property which will be used as Id
         DiscriminateSubClassesOnColumn("TYPE");
    } 
}

public void CareMap : SubclassMap<Car>
{
    public CarMap()
    {
         DiscriminatorValue("CAR");
    }
}

Additionally, I needed to disable lazy loading of the Vehicle from the driver mapping

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2024-11-11 06:27:53

您需要决定您的继承映射将如何工作。请参阅文档 http://nhibernate.info/doc/nh/en/index。 html#inheritance

完成后,您可以映射 Driver 类以级联 Vehicle 依赖项。之后,NHibernate 将自动从数据库加载正确的项目。

更新

您需要将类型名称保留为字符串。那么您的 Vehicle 类可以是一个未映射的属性,其功能如下:

public class Driver
{
    private string vehicleTypeName;
    private IVehicle vehicle;

    public IVehicle Vehicle
    {
        get
        {
            if (vehicle == null)
            {
                vehicle = typeof(IVehicle).Assembly
                                   .CreateInstance(vehicleTypeName);
            }
            return vehicle;
        }
    }
}

You need to decide how your inheritance mapping will work. See the documentation http://nhibernate.info/doc/nh/en/index.html#inheritance

Once you do, you can map your Driver class to cascade the Vehicle dependency. After that point NHibernate will automatically load the proper item from the database.

UPDATE

You will need to persist the type name as a string. Then your Vehicle class can be an unmapped property that does something like:

public class Driver
{
    private string vehicleTypeName;
    private IVehicle vehicle;

    public IVehicle Vehicle
    {
        get
        {
            if (vehicle == null)
            {
                vehicle = typeof(IVehicle).Assembly
                                   .CreateInstance(vehicleTypeName);
            }
            return vehicle;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文