使用 nHibernate 将两种不同的数据模型映射到一个实体模型

发布于 2024-09-01 17:38:48 字数 1062 浏览 6 评论 0原文

我有两个不同的数据模型映射到同一个汽车实体。我需要创建第二个名为 ParkedCar 的实体,它与 Car 相同(因此继承自 Car),以阻止 nhibernate 抱怨同一实体存在两个映射。

public class Car
{
     protected Car()
     {
       IsParked = false;
     }

    public virtual int Id { get; set; }  
    public  bool IsParked { get; internal set; }
}

public class ParkedCar : Car
{
        public ParkedCar()
        {
            IsParked = true;
        }
       //no additional properties to car, merely exists to support mapping and signify the                           car is parked
}

唯一的问题是,当我使用 Criteria API 从数据库中检索汽车时,如下所示:

SessionProvider.OpenSession.Session.CreateCriteria<Car>()
                    .Add(Restrictions.Eq("Id", 123))
                    .List<Car>();

查询返回来自 ParkedCar 数据模型的汽车实体。就好像 nhibernate 默认为专门的实体一样。而且映射绝对是在正确的位置:

<class name="Car" xmlns="urn:nhibernate-mapping-2.2" table="tblCar">

<class name="ParkedCar" xmlns="urn:nhibernate-mapping-2.2" table="tblParkedCar" >

我该如何阻止这种情况?

I have two different data models that map to the same Car entity. I needed to create a second entity called ParkedCar, which is identical to Car (and therefore inherits from it) in order to stop nhibernate complaining that two mappings exists for the same entity.

public class Car
{
     protected Car()
     {
       IsParked = false;
     }

    public virtual int Id { get; set; }  
    public  bool IsParked { get; internal set; }
}

public class ParkedCar : Car
{
        public ParkedCar()
        {
            IsParked = true;
        }
       //no additional properties to car, merely exists to support mapping and signify the                           car is parked
}

The only issue is that when I come to retrieve a Car from the database using the Criteria API like so:

SessionProvider.OpenSession.Session.CreateCriteria<Car>()
                    .Add(Restrictions.Eq("Id", 123))
                    .List<Car>();

The query brings back Car Entities that are from the ParkedCar data model. Its as if nhibernate defaults to the specialised entity. And the mappings are defiantly looking in the right place:

<class name="Car" xmlns="urn:nhibernate-mapping-2.2" table="tblCar">

<class name="ParkedCar" xmlns="urn:nhibernate-mapping-2.2" table="tblParkedCar" >

How do I stop this?

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

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

发布评论

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

评论(2

蓝海 2024-09-08 17:38:48

我认为您需要在 类映射< /a>

<class "Car" polymorphism="explicit" ...

I think you need to set the polymorphism property on the class mapping

<class "Car" polymorphism="explicit" ...
水中月 2024-09-08 17:38:48

由于 ParkedCar 扩展了 Car,因此对 Car 的查询将返回 Car 和 ParkedCar 对象。您可以使用 特殊类属性,即来自 Car c,其中 c.class = Car。我不认为你可以使用标准 API 来做到这一点。

或者,如果列表大小合理,您可以在检索列表后对其进行过滤。

Since ParkedCar extends Car, a query for Car will return both Car and ParkedCar objects. You can restrict the type using HQL using the special class property, i.e. from Car c where c.class = Car. I don't think you can do this with the criteria API.

Alternatively you could filter the list after retrieving it if it's a reasonable size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文