0

i search the web for answer, but all the answers refer to composite id PK. i want two map two columns of type long to PK. one should be regular generated id, and the other should be regular long field.

i have the following mapping:

  <class name="com.company.MyTable" table="My_Table">
        <id name="id" column="id">            
            <generator class="assigned"/>
        </id>
        <property name="jobId" column="job_id" type="long" index="oes_job_id_idx" />
        <property name="serverId" column="server_id" type="long"/>
     </class>

i want to add to the PK the job_id column. how do i do that?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
yonatan
  • 177
  • 1
  • 2
  • 14

2 Answers2

2

Primary keys, by definition should be the unique key with the fewest columns possible:

  • your can't have multiple primary keys
  • you shouldn't use an additional column for the primary key, if the first column is already unique

It doesn't really give you a benefit to create a separate index either - so stick to the generated field as primary key. So, hibernate doesn't support that because it is a wrong thing to do.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • currently i have a bug, on production application, and that is the only solution... i have two tables, one backup the another. when backup the table, we copy also the id (assigned generator). after deleting the original table, it can contains the same id as an old row. when backup again, i will fail on duplicate key. therefore, i need another specific column as PK – yonatan Oct 09 '11 at 11:41
  • @yonatan then the id is _not_ auto-generated. Then you can simply use composite IDs – Bozho Oct 09 '11 at 14:17
0

Please look at this questions there is a full solution for composite Primary keys:

Mapping same class relation

And then

Mapping same class relation - continuation

Hope it helps.

Community
  • 1
  • 1
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200