• 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

Id key in .hbm.xml

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to create a hibernate mapping for a table which doesn't have an Id column as such. Say I have Color table in SQL Server with columns - Nickname and Color. So I am doing it like this It gives mapping error.

Now I uncomment id part - and add id to the bean Color class also.
But when I am trying to do It is giving error as the query is trying to fetch id column from the table Color which doesn't have any column as such.

Would someone please help me out here. TIA
 
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
Enities in a relational database without a primary key are invalid; they are not relational data. This is because in order to participate in a relationship all rows in the entity need to be uniquely identifiable. This is the purpose of the primary key.

Hibernate or an Object Relational Mapping tool. If the data you are trying to map is not relational you can't use Hibernate to map that entity.

There are partial work arounds for legacy data sets, such as mapping every column in the table as a comosite key. But the easiest fix is to add a surrogate key to your table.
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul, very clearly made the point. However if a table is on DB2(AS400 machine), has 65K records in it , about 10 logicals defined on this file(table), 100s of programs accessing and manipulating this file with earlier attribute-list. How can we add surrogate key without disturbing any of these? If any link on how to add this surrogate key - will be much helpful. TIA.
 
Paul Sturrock
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
The number of records don't matter do they? And DB2 supports auto generation of keys so you should be able to add a surrogate key without being too disruptive. The problem you will possibly find is where the developers of other applications have been sloppy and not named the attributes in insert and select statements, at which point they will fail.

If its too much risk to fix the data model then you will have to use native SQL from Hibernate rather than trying to map an object that is not relational.

Adding primary keys should be in your DB2 documentation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic