• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to create hbm files for a table which does not have a primary key.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I want to create a .hbm file for a table which does not have a primary key.
Can anybody help me on this.

Thanks in advance..
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Satyam,

As per Hibernate spec, a entity has to have a primary key if it has to be persisted.

Not sure what sort of requirement you have. If you are trying to save an object (without primary key field) which is of value type to another object, than you can save it without primary key also. But if the object is stand alone parent object than you need to have a primary key.

Please furnish more details, probably your POJO's to help you with the implementation.

Regards,
Naresh Waswani
 
satyam nigam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naresh,

Many thanks for your reply.
I am trying to converting an existing application into Java. Currently it is written in VB. It has a database in MS SQL and I want to use Hibernate to access the database.
But in the database I have found that one table does not have any primary key, so I am just confused on how to make a .hbm file for that table. Also that table does not have any foreign key in it.

Is it possible in hibernate to map this table without the primary key. If I do so I am getting the exception.


Thanks,
Satyam
 
Waswani Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in that case, I am afraid the answer is no

Regards,
Naresh Waswani
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are workarounds for this. You can define the all properties as a composite key. It means all rows now have to be unique, but this probably is the case anyway (you have no other way to uniquely identify a row otherwise). Entities without primary keys are cannot be relational data - these are arguably invalid in the data model as is regardless of whether you use Hibernate.

The easiest way to fix this is to add a surrogate key to this table.
 
All of the following truths are shameless lies. But what about this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic