当外键列位于连接表上时引用连接实体
我有以下类映射: 班级书籍:
Table("BOOKS")
Id(b => b.Id)
Join("BOOKS_EXTRA_INFO", j =>
{
j.Optional();
j.References(b => b.Author);
}
班级作者:
TABLE("AUTHORES")
Id(a => a.Id)
HasMany(a => a.Books).Table("BOOKS_EXTRA_INFO")
此映射的问题是,当我尝试执行以下操作时:
var a = Session.Get<Author>(1);
图书集合为空。我检查了NH生成的SQL,问题出在where子句中:
Select ... WHERE BOOKS.AUTHOR_ID = 1
正确的Where子句应该是:
Select ... WHERE BOOKS_EXTRA_INFO.AUTHOR_ID = 1
我发现这个问题:Hibernate <设置> key from join table 这与我的问题非常相似(如果不相同),但解决方案使用 xml 映射,并且我不明白如何在流畅映射中执行相同的操作。
我无法更改数据库架构。
对于我的问题来说,正确的流畅映射是什么?
编辑:更新了映射以显示我拥有的确切映射。
I have the following class mappings:
Class Book:
Table("BOOKS")
Id(b => b.Id)
Join("BOOKS_EXTRA_INFO", j =>
{
j.Optional();
j.References(b => b.Author);
}
Class Author:
TABLE("AUTHORES")
Id(a => a.Id)
HasMany(a => a.Books).Table("BOOKS_EXTRA_INFO")
The problem with this mapping is that when I try to do the following:
var a = Session.Get<Author>(1);
The Books Collection is empty. I checked the SQL that NH generates and the problem is in the where clause:
Select ... WHERE BOOKS.AUTHOR_ID = 1
The correct Where clause should be:
Select ... WHERE BOOKS_EXTRA_INFO.AUTHOR_ID = 1
I found this question: Hibernate <set> key from joined table which is really similar to (if not same as) my question, but the solution uses xml mappings, and I don't understand how to do the same in fluent mapping.
I Can't change the DB schema.
What is the correct fluent mapping for my problem?
Edit: Updated the mappings to show the exact mappings I have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您链接的答案指定集合上的表名。这可以流利地完成:
The answer you linked specifies the table name on the set. This can be done in Fluent easily: