permaculture playing cards*
The moose likes Object Relational Mapping and the fly likes How to create hbm files for a table which does not have a primary key. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to create hbm files for a table which does not have a primary key." Watch "How to create hbm files for a table which does not have a primary key." New topic
Author

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

satyam nigam
Greenhorn

Joined: Jun 10, 2011
Posts: 2
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..
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
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


Naresh Waswani
satyam nigam
Greenhorn

Joined: Jun 10, 2011
Posts: 2
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

Joined: May 01, 2008
Posts: 66
Well in that case, I am afraid the answer is no

Regards,
Naresh Waswani
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to create hbm files for a table which does not have a primary key.