Hibernate——将具有单个值的视图映射到只读字段
(Hibernate 3.6,Mysql 5)
我有一个名为“Program”的映射类(工作正常)。
我还有一个每天更新汇总统计数据的视图。
视图模式如下:
view ProgramSummary {
long program_id
long value
}
program_id 是唯一的,但没有 row-id。我想要的只是提取值,但我似乎无法找出正确的语义。
我尝试过使用 OneToOne(和 ManyToOne)关系映射一个单独的实体,但 id 之间会出现混淆。如果不将program_id标记为实体,hibernate将无法处理实际的映射。
有没有一种简单的方法可以做到这一点?它是一个由外部进程访问的只读字段,我没有任何更改架构的余地。
(Hibernate 3.6, Mysql 5)
I have a mapped class called 'Program' (that is working correctly).
I also have a view that is updated daily with aggregate statistics.
The view schema is like so:
view ProgramSummary {
long program_id
long value
}
program_id is unique, but there is no row-id. What I want is to just pull the value out, but I can't seem to figure out the right semantics.
I have tried mapping a separate entitiy with a OneToOne (and ManyToOne) relationship, but it gets confused between the ids. Without marking program_id as an entity, hibernate can't handle the actual mapping.
Is there a simple way to do this? It is a read-only field that is accessed by an external process and I don't have any leeway for changing the schema.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用程序 hbm 文件中的公式来映射它。
假设您的字段名为 SummaryValue,此行将更新 Program 对象中的该字段。任何使用公式映射的字段都不可更新。
You can use a formula in your Program hbm file to map this.
Assuming your field is named SummaryValue this line will update that field in the Program object. Any field mapped with a formula is not updateable.
如果您映射第二个实体,您可能需要将列设置为
updatable=false
和insertable=false
,使其只读。但是,为什么不直接从 ProgramSummary 中选择值(可能使用纯 SQL)呢?
If you map a second entity, you might want to set the columns to
updatable=false
andinsertable=false
, making them readonly.However, why don't you just select the value from ProgramSummary, probably with plain SQL?