NHibernate:在连接表上映射连接

发布于 2024-11-26 21:14:01 字数 909 浏览 1 评论 0原文

我有一个视图将 3 个表连接到一个链中,我需要将其替换为 NHibernate 映射,而不需要对数据库进行任何更改。这可能吗? 这是我的观点的一个简化示例:

SELECT tblTable1.*,tblTable2.*,tblTable3.MyProperty FROM tblTable1
OUTER JOIN
tblTable2 ON tblTable1.Table1Key = tblTable2.Table1Key
OUTER JOIN
tblTable3 ON tblTable2.Table2Key = tblTable3.Table2Key

所以基本上我们选择 tblTable1 并加入 tblTable2,这在 NHibernate 中对我有用。我的问题是 tblTable3。如何将其连接到连接表 tblTable2 的属性上?

当我进行这样的映射时,由于某种原因,我收到一个尝试将 tblTable3 连接到 Table1Key 的查询。

<class name="MyClass" table="tblTable1">

<id name="Table1Key">
  <generator class="identity"/>
</id>

<property name="..." />

<join table="tblTable2">
  <key column="Table1Key" />

  <property name="..." />
</join>

<join table="tblTable3">
  <key column="Table2Key???" />

  <property name="..." />
</join>

</class>

I have a view joining 3 tables together in a chain that i need to replace with a NHibernate mapping without any changes to the database. Is this possible?
This is a simplified example of my view:

SELECT tblTable1.*,tblTable2.*,tblTable3.MyProperty FROM tblTable1
OUTER JOIN
tblTable2 ON tblTable1.Table1Key = tblTable2.Table1Key
OUTER JOIN
tblTable3 ON tblTable2.Table2Key = tblTable3.Table2Key

so basicaly we select tblTable1 and join tblTable2 this works for me in NHibernate. My problem is tblTable3. How do i join it on a property from the joined table tblTable2?

When i do the mapping like this i get a query trying to join tblTable3 on Table1Key for some reason.

<class name="MyClass" table="tblTable1">

<id name="Table1Key">
  <generator class="identity"/>
</id>

<property name="..." />

<join table="tblTable2">
  <key column="Table1Key" />

  <property name="..." />
</join>

<join table="tblTable3">
  <key column="Table2Key???" />

  <property name="..." />
</join>

</class>

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

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

发布评论

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

评论(2

忆沫 2024-12-03 21:14:01

在 NH 中,您无法加入 join。唯一的方法是使它成为一个引用,这基本上做同样的事情

<class name="MyClass" table="tblTable1">
  <id name="Table1Key">
    <generator class="identity"/>
  </id>

  <property name="..." />

  <many-to-one table="tblTable2" lazy="false">
    <key column="Table1Key" />

    <property name="..." />
  </many-to-one>
</class>

<class name="MyClass2" table="tblTable2">

  <join table="tblTable3">
    <key column="Table2Key" />

    <property name="..." />
  </join>
</class>

class MyClass
{
    public virtual MyClass2 MyClass2 { get; set; }

    public virtual int MyClass2_MyProperty
    {
        get { return MyClass2.MyProperty; }
        set { MyClass2.MyProperty = value; }
    }
}

class MyClass2
{
    public virtual int MyProperty { get; set; }
}

编辑选项2:如果你不想要MyClass2也许你可以调整这个:

<class name="MyClass" table="tblTable2">    <-- take table2 as the main table
  <id name="Table1Key">
    <generator class="sequence">  <-- can be problem here, use sequence (directly) instead?
      <param name="sequencename">table1_id_squence</param> // not sure about "sequencename"
    </generator>
  </id>

  <property name="..." />

  <join table="tblTable1">
    <key column="Table1Key" />

    <property name="..." />
  </join>

  <join table="tblTable3">
    <key column="Table2Key" />

    <property name="..." />
  </join>
</class>

in NH you can't join in a join. the only way is to make it a reference, which does basicly the same thing

<class name="MyClass" table="tblTable1">
  <id name="Table1Key">
    <generator class="identity"/>
  </id>

  <property name="..." />

  <many-to-one table="tblTable2" lazy="false">
    <key column="Table1Key" />

    <property name="..." />
  </many-to-one>
</class>

<class name="MyClass2" table="tblTable2">

  <join table="tblTable3">
    <key column="Table2Key" />

    <property name="..." />
  </join>
</class>

class MyClass
{
    public virtual MyClass2 MyClass2 { get; set; }

    public virtual int MyClass2_MyProperty
    {
        get { return MyClass2.MyProperty; }
        set { MyClass2.MyProperty = value; }
    }
}

class MyClass2
{
    public virtual int MyProperty { get; set; }
}

Edit Option 2: if you dont want MyClass2 Maybe you can tweak this:

<class name="MyClass" table="tblTable2">    <-- take table2 as the main table
  <id name="Table1Key">
    <generator class="sequence">  <-- can be problem here, use sequence (directly) instead?
      <param name="sequencename">table1_id_squence</param> // not sure about "sequencename"
    </generator>
  </id>

  <property name="..." />

  <join table="tblTable1">
    <key column="Table1Key" />

    <property name="..." />
  </join>

  <join table="tblTable3">
    <key column="Table2Key" />

    <property name="..." />
  </join>
</class>
流星番茄 2024-12-03 21:14:01

没有尝试这个,但也许您可以在第二个连接上定义一个属性,并在第三个连接中使用属性引用作为键列

  <join table="tblTable2">
    <key column="Table1Key" />
    <property name="Table2Key" />
  </join>
  <join table="tblTable3">
    <key property-ref="Table2Key" />
    <property name="..." />
  </join>

Didn't try this, but maybe you can define a property on your second join an use a property-ref for key column in your third join

  <join table="tblTable2">
    <key column="Table1Key" />
    <property name="Table2Key" />
  </join>
  <join table="tblTable3">
    <key property-ref="Table2Key" />
    <property name="..." />
  </join>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文