• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding to a table with no primary keys  RSS feed

 
JP Estrada
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table that i need to map that does not have primary keys. The only 2 fields are used in many-to-one relationships.
So i used a composite id in my mapping:


<hibernate-mapping>
<class name="com.middle_table" table="middle_table">
<composite-id>
<key-property name="int_col1_id">
<column name="COL1"/>
</key-property>
<key-property name="int_col2_id">
<column name="COL2"/>
</key-property>
</composite-id>

<many-to-one name="col1_id" class="com.Col1" />
<many-to-one name="col2_id" class="com.Col2" />
</class>
</hibernate-mapping>


I added 2 int versions of the actual db field to my pojo. ex. Since col1_id is already used for a many-to-one association, i created an int_col1_id..

Now i'm having problems because i can't insert a new record into the table. It's saying col1_id is specified twice..

What is the right way to map a table with no primary keys in such way that i can insert new records into it?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the right way to map a table with no primary keys in such way that i can insert new records into it?

There isn't one. A relational entity must have a primary key, otherwise it can't participate in relationships, and ORMs can't map it. Could you not just add a surrogate key?
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the OP has a primary key, its just a two column composite key.

Normally you'd declare an inner class that wraps the two attributes of the composite key, and not add the field twice.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!