10

Possible Duplicate:
Hibernate and no PK

Anyone knows how to do hibernate mapping for table or view without a primary key?

Community
  • 1
  • 1
ariso
  • 1,433
  • 6
  • 21
  • 35

2 Answers2

10

Don't think Hibernate allows mapping a table without a primary key...think about how Hibernate would perform updates without a column that can uniquely identify a row.

I guess a work-around would be to use a composite key with all columns, but you are much better off adding a primary key.

Ryan Anderson
  • 502
  • 5
  • 16
  • I think you are correct here. Also the composite key (of all columns) would still need to be primary, so your better off making a more reasonable composite key or choosing a uniquely identifying column as the primary key. – James McMahon May 21 '09 at 19:30
  • 7
    I don't know that it matters too much in a read-only situation (wrt udpates). – javamonkey79 Nov 12 '10 at 00:04
  • 4
    so,when I select from a view without a primary key ,and dont want t oupdate view what should I do? – DeveloperX Feb 08 '14 at 15:50
5

I would do this only when you are reading data (not writing it). When you have a DB like oracle, you can have statements like

select DOKUMENT.*, ROWID from DOKUMENT

→ and thus, you can add this statement into the Hibernate mapping:

<id column="ROWID" type="string" />

subsequently, you define all other columns as

<property...

When you use the reverse engineering Wizard, you can

  1. remove the composite-key tag,
  2. search and replace key-property for property and
  3. insert above line
Marko
  • 20,385
  • 13
  • 48
  • 64