Hibernate mapping for a table with no primary key
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.
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?
Also, this is an Oracle db: is there some autogenerated id I c开发者_运维技巧an use despite the (possibly) poor db design?
- 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)
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
:
<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.
Reference
- 8.4. Components as composite identifiers
精彩评论