views:

35

answers:

2

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 can use despite the (possibly) poor db design?

+2  A: 
  1. Always add a PK.
  2. 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.
  3. Hibernate does handle this for you. Just add an ID column and map it as "native" (see the docs)
Aaron Digulla
1. Not my table, but I will try to heed your advice.2. Constraints should cascade the change, shouldn't they?3. I meant the question more along the lines of, "is there a hidden pk I can use," and I think you answered the question, "When I have a pk sequence, will Hibernate automatically figure out what type it is?" Am I reading the docs well enough or did I misunderstand your intent?
Nathan Spears
1. If you can, have the guy who made that DB design fired. He's dangerous. ;-) 2. They can if you enable that (see the mapping docs). 3. I can't see all the columns but I guess you'd posted any PK column if you could find it, so I assume there isn't one. The type is `NUMBER(20)` or similar. Don't hesitate to use a large number unless you have > 100 million rows.
Aaron Digulla
+1  A: 

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

Pascal Thivent
This is what I had done by the time you posted, so I'll give you the answer. Ideal is ideal but sometimes you have to get work done anyway.
Nathan Spears
@Nathan That was the spirit of my answer: it can possibly work but it's not ideal :) If you can get the problem (no PK) fixed, it would be good.
Pascal Thivent