This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
Joined: Jun 10, 2011
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.
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.