不带唯一标识符的 NHibernate 映射 SQL-VIEW
我有一个只读 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的映射文件应如下所示:
它是否正确映射到您的视图?如果没有,请告诉我您还尝试过什么。
Your mapping file should look like:
Does that map correctly to your view? Please let me know what else you've tried if not.
感谢安迪,这引导我找到了最终的解决方案。
我将把 hbm.xml 模板与我的域模型实体放在一起。
我映射的所有列都是 varchars/strings(因此我的复合 ID 由两个字符串组成):
但是映射还不够。 Nhibernate 请求重写
Equals
和GetHashCode
函数,我将放置与此 XML 相对应的工作 VB.Net 类的确切代码: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):
But the mapping was not enough. Nhibernate requests to Override the
Equals
andGetHashCode
function, I will put the exact code of my working VB.Net class that correspond to this XML: