Java 如何在没有主键的情况下为表或视图进行休眠映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/894610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do hibernate mapping for table or view without a primary key
提问by ariso
Possible Duplicate:
Hibernate and no PK
可能的重复:
休眠且无 PK
Anyone knows how to do hibernate mapping for table or view without a primary key?
任何人都知道如何在没有主键的情况下为表或视图进行休眠映射?
采纳答案by Ryan Anderson
Don't think Hibernate allows mapping a table without a primary key...think about how Hibernate would perform updates without a column that can uniquely identify a row.
不要认为 Hibernate 允许在没有主键的情况下映射表......想想 Hibernate 如何在没有可以唯一标识行的列的情况下执行更新。
I guess a work-around would be to use a composite key with all columns, but you are much better off adding a primary key.
我想一种解决方法是对所有列使用复合键,但最好添加主键。
回答by Sebastian Rothbucher
I would do this only when you are reading data (not writing it). When you have a DB like oracle, you can have statements like
我只会在您读取数据(而不是写入数据)时执行此操作。当你有一个像 oracle 这样的数据库时,你可以有像这样的语句
select DOKUMENT.*, ROWID from DOKUMENT
→ and thus, you can add this statement into the Hibernate mapping:
→ 因此,您可以将此语句添加到 Hibernate 映射中:
<id column="ROWID" type="string" />
subsequently, you define all other columns as
随后,您将所有其他列定义为
<property...
When you use the reverse engineering Wizard, you can
使用逆向工程向导时,您可以
- remove the composite-key tag,
- search and replace key-property for property and
- insert above line
- 删除复合键标签,
- 搜索并替换 key-property 的属性和
- 在行上方插入