不带唯一标识符的 NHibernate 映射 SQL-VIEW

发布于 2024-12-10 00:44:29 字数 593 浏览 0 评论 0原文

我有一个只读 mssql-视图,我想将其映射到nhibernate(使用hbm.xml 文件)。 该视图是连接两个表的选择。为了给出抽象的洞察力,选择是这样的:

SELECT A.Id As A_ID, B.Id As B_ID,
       A.AttributeA, B.AttributeB,

FROM A INNER JOIN B ON 
     A.Id = B.RootID

它是 A 和 B 之间的一对多关系(B 条目是 A 条目的依赖/叶子)。

我正在使用 nhibernate 的 hbm.xml 文件,但无法使其工作。

如果有人能告诉我我必须使用哪种 XML,我会很高兴,我想由于我的视图没有 ID,我必须创建一个复合 nhibernate id(这将是 os A 和 B 的 id)在一起)但我无法让它发挥作用。 此外,该视图是只读的,所以我认为这应该会使解决方案更容易。

我问这个问题是因为网站上的其他人都没有回答这个问题(有些使用 Fluent-nhibernate,我使用 XML 映射文件)

提前致谢。

I have a read-only mssql-view, and I want to map it to nhibernate (using hbm.xml files).
The view is a select that joins two tables. For give an abstract-insight, the select is something like this:

SELECT A.Id As A_ID, B.Id As B_ID,
       A.AttributeA, B.AttributeB,

FROM A INNER JOIN B ON 
     A.Id = B.RootID

It's a one-to-many relationship between A and B (B entries are dependant/leafs of A entries).

I'm using nhibernate's hbm.xml files, and I can't make it to work.

I'd gladly appreciate if someone can enlight me of the kind of XML that I have to use, I guess that as my view has no ID, I have to create a composite-nhibernate id (wich will be the ids os A and B together) but I couldn't make it to work.
Also the view is READ ONLY so I think that should make the solution easier.

I'm asking the question because none of the others on the site answered this issue (some use fluent-nhibernate,I'm with XML mapping files)

thanks in advance.

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

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

发布评论

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

评论(2

撩发小公举 2024-12-17 00:44:29

您的映射文件应如下所示:

<class name="blah" mutable="false">
  <composite-id>
    <key-property name="A_ID"/>
    <key-property name="B_ID"/>
  </composite-id>

  <property name="A.Attribute" />
  <property name="B.Attribute" />
</class>

它是否正确映射到您的视图?如果没有,请告诉我您还尝试过什么。

Your mapping file should look like:

<class name="blah" mutable="false">
  <composite-id>
    <key-property name="A_ID"/>
    <key-property name="B_ID"/>
  </composite-id>

  <property name="A.Attribute" />
  <property name="B.Attribute" />
</class>

Does that map correctly to your view? Please let me know what else you've tried if not.

混吃等死 2024-12-17 00:44:29

感谢安迪,这引导我找到了最终的解决方案。
我将把 hbm.xml 模板与我的域模型实体放在一起。
我映射的所有列都是 varchars/strings(因此我的复合 ID 由两个字符串组成):

    <class name="Model.Projects.ProjectsLoansView"
           table="V_PROJECTSLOANS" mutable="false">

    <composite-id>
      <key-property name="Number" column="Number"/>
      <key-property name="OperationNumber" column="OperationNumber" />
    </composite-id>

    <property name="Name" column="Name" not-null="false"/>

  </class>

但是映射还不够。 Nhibernate 请求重写 EqualsGetHashCode 函数,我将放置与此 XML 相对应的工作 VB.Net 类的确切代码:

  Public Class ProjectsLoansView

        Public Overridable Property Number As String
        Public Overridable Property OperationNumber As String

        Public Overridable Property Name As String

        Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean

            If (IsNothing(obj)) Then Return False

            Dim t As ProjectsLoansView = CType(obj, ProjectsLoansView)

            If ((Me.Number = t.Number) And (Me.OperationNumber = t.OperationNumber)) Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Overrides Function GetHashCode() As Integer
            'TODO: check how to override properly GetHashCode. As is a-read only view it doesn't matter here
            Return MyBase.GetHashCode()
        End Function

    End Class

Thanks to Andy, that lead me to the final solution.
I will put the hbm.xml template with my domain-model entities.
All the columns I'm mapping are varchars/strings (hence my composite ID consist of two strings):

    <class name="Model.Projects.ProjectsLoansView"
           table="V_PROJECTSLOANS" mutable="false">

    <composite-id>
      <key-property name="Number" column="Number"/>
      <key-property name="OperationNumber" column="OperationNumber" />
    </composite-id>

    <property name="Name" column="Name" not-null="false"/>

  </class>

But the mapping was not enough. Nhibernate requests to Override the Equals and GetHashCode function, I will put the exact code of my working VB.Net class that correspond to this XML:

  Public Class ProjectsLoansView

        Public Overridable Property Number As String
        Public Overridable Property OperationNumber As String

        Public Overridable Property Name As String

        Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean

            If (IsNothing(obj)) Then Return False

            Dim t As ProjectsLoansView = CType(obj, ProjectsLoansView)

            If ((Me.Number = t.Number) And (Me.OperationNumber = t.OperationNumber)) Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Overrides Function GetHashCode() As Integer
            'TODO: check how to override properly GetHashCode. As is a-read only view it doesn't matter here
            Return MyBase.GetHashCode()
        End Function

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