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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:46:51  来源:igfitidea点击:

how to do hibernate mapping for table or view without a primary key

javahibernatejdbc

提问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

使用逆向工程向导时,您可以

  1. remove the composite-key tag,
  2. search and replace key-property for property and
  3. insert above line
  1. 删除复合键标签,
  2. 搜索并替换 key-property 的属性和
  3. 在行上方插入