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.
posted 9 years ago
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.