yonatan
yonatan

Reputation: 187

hibernate - mapping PK to 2 colums

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?

Upvotes: 0

Views: 194

Answers (2)

danny.lesnik
danny.lesnik

Reputation: 18639

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.

Upvotes: 0

Bozho
Bozho

Reputation: 597114

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.

Upvotes: 2

Related Questions