Hibernation mapping for a table without a primary key

I know this question has been asked before, but I also have a design question.

There are two tables:

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 is easy to work with this structure, but I have two questions. First, how do I display group_members in a hibernate mapping? Hibernate wants some sort of identifier, and I'm not sure what to say.

Secondly, although maybe I should first ask about this, is this a bad db design? Should there be pk in the group_members table, like a sequence or something else?

Also, this is Oracle db: is there some kind of auto-generated id that I can use despite the (possibly) bad db design?

+4
source share
2 answers

You absolutely need an identifier in the mapping that describes how the string is unique: using PK, assigned, or compound. In your case, you can use 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 that the pair (fk_group_name, group_member) is unique. But this is not ideal, you must have a PC.

Link

+2
source
  • Always add a PC.
  • Never use a business key (such as a name) as a foreign key. What happens if a user marries? Or is the group renaming itself? Always use a PC.
  • Hibernate does this for you. Just add an identifier column and map it as "native" ( see Docs ).
+3
source

All Articles