• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding to a table with no primary keys

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic