NHibernate 使用相同的 Id 将对象映射到多个表?

发布于 2024-12-28 01:18:52 字数 673 浏览 2 评论 0原文

我想要执行以下操作,想象一下您有一个对象的场景:

public class ObjectA {
    public virtual Guid Id;
    public virtual string PropertyA;
    public virtual string PropertyB;
    public virtual string PropertyC;
    public virtual string PropertyD;
}

出于我不会详细讨论的原因,数据库需要如下所示:

----
Table: ObjectABase
----
Column Id
Column PropertyA
Column PropertyB
----

----
Table: ObjectAExtended
----
Column Id
Column PropertyC
Column PropertyD
----

该对象在两个表之间拆分,并由相同的 Id 引用,因此当你调用 Session.Save(... 它将保存到两个表中。

有人知道如何做到这一点或者甚至可能吗?

干杯。

编辑:我已经找到了答案,但不能再过7小时显然会更新。 明天。

I'm looking to do the following, imagine a scenario where you have an object:

public class ObjectA {
    public virtual Guid Id;
    public virtual string PropertyA;
    public virtual string PropertyB;
    public virtual string PropertyC;
    public virtual string PropertyD;
}

And for reasons I wont go into, the database NEEDS to look like this:

----
Table: ObjectABase
----
Column Id
Column PropertyA
Column PropertyB
----

----
Table: ObjectAExtended
----
Column Id
Column PropertyC
Column PropertyD
----

The object is split between both tables and is referenced by the same Id so when you call Session.Save(... it will save to both tables.

Anyone any ideas how to do this or if its even possible?

Cheers.

EDIT: I have figured out the answer but can't post it for another 7 hours apparently. Will update tomorrow.

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

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

发布评论

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

评论(2

半透明的墙 2025-01-04 01:18:52

我想通了。在 NHibernate 3.2 中使用代码映射...

public class ObjectAMap : ClassMapping<ObjectA> {
    public ObjectAMap () {
        Table("ObjectABase");
        Id<Guid>(x => x.Id, m => { m.Column("Id"); });
        Property(x => x.PropertyA, map => { map.Column("PropertyA"); });
        Property(x => x.PropertyB, map => { map.Column("PropertyB"); });
        Join("ObjectA",
             m => {
                 m.Table("ObjectAExtended");
                 m.Key(x => { x.Column("Id"); });
                 m.Property<string>(x => x.PropertyC, map => { map.Column("PropertyC"); });
                 m.Property<string>(x => x.PropertyD, map => { map.Column("PropertyD"); });
             });

        }); 
    }
}

I figured it out. In NHibernate 3.2 using map by code...

public class ObjectAMap : ClassMapping<ObjectA> {
    public ObjectAMap () {
        Table("ObjectABase");
        Id<Guid>(x => x.Id, m => { m.Column("Id"); });
        Property(x => x.PropertyA, map => { map.Column("PropertyA"); });
        Property(x => x.PropertyB, map => { map.Column("PropertyB"); });
        Join("ObjectA",
             m => {
                 m.Table("ObjectAExtended");
                 m.Key(x => { x.Column("Id"); });
                 m.Property<string>(x => x.PropertyC, map => { map.Column("PropertyC"); });
                 m.Property<string>(x => x.PropertyD, map => { map.Column("PropertyD"); });
             });

        }); 
    }
}
巷雨优美回忆 2025-01-04 01:18:52

一种可能性是在 nhibernate 映射中使用连接子类元素。您可以按如下方式布置对象:

public class ObjectABase
{
   public virtual int Id {get; set;}
   public virtual string PropertyA {get; set}  
   public virtual string PropertyB {get; set}  
}

public class ObjectA : ObjectABase
{
   public virtual string PropertyC {get; set}  
   public virtual string PropertyD {get; set}    
}

然后您可以按如下方式设置映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="Company.DomainModel">
  <joined-subclass name="ObjectA" table="ObjectAExtended" extends="Company.DomainModel.ObjectABase" lazy="false">
    <key column="Id" />
    <property name="PropertyC" />
    <property name="PropertyD" />
  </joined-subclass>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Company.DomainModel" assembly="DomainModel">
  <class name="ObjectABase" table="ObjectABase" lazy="false">
    <id name="Id" column="Id">
      <generator class="identity"/>
    </id>
    <property name="PropertyA" />
    <property name="PropertyB" />
  </class>
</hibernate-mapping>

通过关闭延迟加载,您可以一次性从数据库中获取所有内容。

这并不完全是您在问题中指定的内容,因为 ObjectA 的属性是在 ObjectA 和继承的基础对象中定义的,但对于您的应用程序来说可能没有实际差异。

One possibility would be to use a joined-subclass element in the nhibernate mapping. You could lay out your objects as follows:

public class ObjectABase
{
   public virtual int Id {get; set;}
   public virtual string PropertyA {get; set}  
   public virtual string PropertyB {get; set}  
}

public class ObjectA : ObjectABase
{
   public virtual string PropertyC {get; set}  
   public virtual string PropertyD {get; set}    
}

and then you could set up a mapping as follows:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="Company.DomainModel">
  <joined-subclass name="ObjectA" table="ObjectAExtended" extends="Company.DomainModel.ObjectABase" lazy="false">
    <key column="Id" />
    <property name="PropertyC" />
    <property name="PropertyD" />
  </joined-subclass>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Company.DomainModel" assembly="DomainModel">
  <class name="ObjectABase" table="ObjectABase" lazy="false">
    <id name="Id" column="Id">
      <generator class="identity"/>
    </id>
    <property name="PropertyA" />
    <property name="PropertyB" />
  </class>
</hibernate-mapping>

By turning off lazy-loading you can get everything from the database in one shot.

This isn't exactly what you specified in your question, since the properties of ObjectA are defined both in ObjectA and the inherited base object, but there might not be a practical difference for your application.

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