hibernate从多个表中的多个列定义类
我有一个带有一些数据字段的类:
class Call{
roomId
roomDisplay
roomLocation
typeId
name
staffAidId
}
现在我想使用 java 中的 hibernate 注释来映射它 但我使用的原始 sql 查询是这样的:
SELECT
c.roomId,
r.display,
r.location,
c.typeId,
c.staffAidId,
s.firstname,
s.lastname
FROM callalert c
JOIN staffmember s
LEFT JOIN roomGroup g ON g.groupId = s.roomGroupId
LEFT JOIN room r ON r.roomId = g.roomId
WHERE s.staffId = 4444 AND c.roomId = g.roomId
有没有办法映射它 换句话说: 有没有办法将不同表中的列映射到 1 个自定义类? 该类不是 1 个表的直接表示 并且从涉及的所有表中,并非所有列都被使用
编辑
我尝试了视图解决方案:
使用上述查询创建了一个视图 然后我尝试使用 Netbeans 生成休眠类 我生成了 2 个类: 类 Calls 和可嵌入类 CallId
在搜索互联网后,
会发生这种情况,因为 hibernate 需要一个主键,因此它自己创建一个主键,我如何确保只生成 1 个类? 如何为视图提供主键(好的主键是基础表的主键之一)。我该如何设置?
i have a class with some datafields:
class Call{
roomId
roomDisplay
roomLocation
typeId
name
staffAidId
}
now i would like to map this using hibernate annotations in java
but the original sql query that i used was something like this:
SELECT
c.roomId,
r.display,
r.location,
c.typeId,
c.staffAidId,
s.firstname,
s.lastname
FROM callalert c
JOIN staffmember s
LEFT JOIN roomGroup g ON g.groupId = s.roomGroupId
LEFT JOIN room r ON r.roomId = g.roomId
WHERE s.staffId = 4444 AND c.roomId = g.roomId
is there a way to map this
in other words:
is there a way to map columns from different tables to 1 custom class?
the class is no direct representation of 1 table
and from all the tables that are involved, not all columns are used
edit
I tried the view solution:
created a view with the above query
then I try to generate the hibernate classes using Netbeans
and I generates 2 classes:
the class Calls and an Embeddable class CallId
after searching the Internet this happens because hibernate needs a primary key so it creates one himself
how can I make sure only 1 class gets generated?
how do I give a view a primary key (a good primary key would be 1 of the primary keys of the underlying tables). how do i set this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您为该查询创建数据库视图,您应该能够做到这一点。它可以工作,但它有一个缺点,即您无法从数据库模型自动生成 ddl 架构。
查看这篇文章
You should be able to do that if you create a database view for that query. It would work, but it has the downside that you can't auto-generate the ddl schema from the database models.
Check out this article