如何使用 nHibernate 映射 n 列主键

发布于 2024-12-17 21:40:25 字数 161 浏览 0 评论 0原文

我有一个包含 2 列作为 PK(复合主键)的表。

如何将它们映射到 hbm.xml 中的“Id”?

<id name="A" />  

我怎样才能用流畅的nhibernate做到这一点?

I have a table with 2 columns as PK (composite primary key).

How can I map them to "Id" in hbm.xml?

<id name="A" />  

How can I do it with fluent nhibernate?

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

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

发布评论

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

评论(3

初雪 2024-12-24 21:40:25

NHibernate 文档描述了如何使用和映射 composite-id。

您还可以使用组件作为compositeid

对于 Fluent NHibernate:

public class ClassNameMap: ClassMap<ClassName>
{
    public ClassNameMap()
    {
        CompositeId().
            .KeyReference(x => x.A, "A")
            .KeyReference(x => x.B, "B");
    }
}

The NHibernate documentation describes how to use and map a composite-id.

You can also use a component as compositeid.

And for Fluent NHibernate:

public class ClassNameMap: ClassMap<ClassName>
{
    public ClassNameMap()
    {
        CompositeId().
            .KeyReference(x => x.A, "A")
            .KeyReference(x => x.B, "B");
    }
}
夜灵血窟げ 2024-12-24 21:40:25

对于 nHibernate 中的复合主键,我建议;

对于 hbm.xml:

<hibernate-mapping>   
<class table="TableName" name="Namespace.ClassName, ClassName">
<composite-id>
    <key-property name="IdPropertyOne" column="ColumnOne" />
    <key-property name="IdPropertyTwo" column="ColumnTwo" />
</composite-id>
<property name="PropertyName" column="ColumnName" type="String"></property>
</class>
</hibernate-mapping>

然后您应该重写 EqualsGetHashCode 方法,以便 nHibernate 知道如何比较您的新密钥并识别不同的对象。这取决于您愿意走多远,您也可以使用“键多对一”将外键映射到这些上。

对于多对一的键:

<key-many-to-one name="PropertyOneObjName" column="ColumnOne" class="PropertyOneClassName">

对于流畅的休眠,复合键如下所示:

public ClassName(){
    CompositeId().
        KeyProperty(x => x.IdPropertyOne,"ColumnOne")
        KeyProperty(x => x.IdPropertyTwo,"ColumnTwo")
}

For a composite primary key in nHibernate I would suggest;

For the hbm.xml:

<hibernate-mapping>   
<class table="TableName" name="Namespace.ClassName, ClassName">
<composite-id>
    <key-property name="IdPropertyOne" column="ColumnOne" />
    <key-property name="IdPropertyTwo" column="ColumnTwo" />
</composite-id>
<property name="PropertyName" column="ColumnName" type="String"></property>
</class>
</hibernate-mapping>

And then you should override Equals and GetHashCode methods, so that nHibernate knows how to compare your new key and identify different objects. It depends on how far you are willing to go, you can as well map foreign keys on these by using "key-many-to-one".

For a key-many-to-one:

<key-many-to-one name="PropertyOneObjName" column="ColumnOne" class="PropertyOneClassName">

For fluent hibernate a composite key looks like:

public ClassName(){
    CompositeId().
        KeyProperty(x => x.IdPropertyOne,"ColumnOne")
        KeyProperty(x => x.IdPropertyTwo,"ColumnTwo")
}
满意归宿 2024-12-24 21:40:25

尝试一下 NHibernate...

<composite-id name="Key" class="KeyClass"> 
  <key-many-to-one name="first" column="firstColumn" lazy="proxy" class="FirstClass"/>
  <key-many-to-one name="second" column="secondColumn" lazy="proxy" class="SecondClass"/>
</composite-id>

以及流畅的 Hibernate

<composite-id>  
  <key-property type="Int32" name="first" column="firstColumn" />  
  <key-property type="Int32" name="second" column="secondColumn" />  
</composite-id>  

希望对您有帮助...:-)

try this for NHibernate...

<composite-id name="Key" class="KeyClass"> 
  <key-many-to-one name="first" column="firstColumn" lazy="proxy" class="FirstClass"/>
  <key-many-to-one name="second" column="secondColumn" lazy="proxy" class="SecondClass"/>
</composite-id>

and for fluent Hibernate

<composite-id>  
  <key-property type="Int32" name="first" column="firstColumn" />  
  <key-property type="Int32" name="second" column="secondColumn" />  
</composite-id>  

hope that helps you...:-)

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