oracle 没有主键的表的休眠映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3500222/
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
Hibernate mapping for a table with no primary key
提问by Nathan Spears
I know this question has been asked before but I have a design question as well.
我知道以前有人问过这个问题,但我也有一个设计问题。
There are two tables like so:
有两张这样的表:
Table Group_table
column pk_Group_name
column group_type
etc
Table Group_members
column fk_group_name -> foreign key to group_table
column group_member
It's easy to work with this structure but I have two questions. First, how should I map group_members in Hibernate mapping? Hibernate wants an id of some sort and I'm not sure what to tell it.
使用这种结构很容易,但我有两个问题。首先,我应该如何在 Hibernate 映射中映射 group_members?Hibernate 需要某种类型的 id,但我不知道该告诉它什么。
Second, although perhaps I should ask it first, is this bad db design? Should there be a pk on the group_members table, like a sequence or something?
其次,虽然也许我应该先问一下,这是糟糕的数据库设计吗?group_members 表上是否应该有一个 pk,例如序列或其他什么?
Also, this is an Oracle db: is there some autogenerated id I can use despite the (possibly) poor db design?
此外,这是一个 Oracle 数据库:尽管(可能)糟糕的数据库设计,我是否可以使用一些自动生成的 ID?
采纳答案by Pascal Thivent
You absolutely need an identifier in the mapping that describes how a row is unique: by PK, assigned, or composite. In your case, you could maybe use a composite-id
:
您绝对需要映射中的标识符来描述行的唯一性:通过 PK、分配或组合。在您的情况下,您可以使用composite-id
:
<class name="eg.Foo" table"FOOS">
<composite-id name="compId" class="eg.FooCompositeID">
<key-property name="string"/>
<key-property name="short"/>
</composite-id>
<property name="name"/>
....
</class>
Of course, this assumes the couple (fk_group_name, group_member) is unique. But this is not ideal, you should have a PK.
当然,这假设这对 (fk_group_name, group_member) 是唯一的。但这并不理想,你应该有一个PK。
Reference
参考
回答by Aaron Digulla
- Always add a PK.
- Never use a business key (like a name) as a foreign key. What happens if a user marries? Or a group renames itself? Always use a PK.
- Hibernate does handle this for you. Just add an ID column and map it as "native" (see the docs)
- 总是添加一个PK。
- 切勿使用业务键(如名称)作为外键。如果用户结婚了会怎样?或者一个组重命名自己?始终使用PK。
- Hibernate 确实为您处理了这个问题。只需添加一个 ID 列并将其映射为“本机”(请参阅文档)