Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate without Primary Key

Tags:

I making a sample application with Hibernate. My requirement is that there is no primary key on the table. I had to do only select query from application. I know there should be a primary key, but the table I am referring has been made without it.

It has about 50k records. So, modifying the table to add ID column does not see viable option.

Can it possible?

like image 708
Ankit Avatar asked Dec 21 '11 14:12

Ankit


People also ask

Can we use Hibernate without primary key?

No, Hibernate will not work without primary key. Every table should have some unique key. The mathematical definition of a relation is a set of tuples.

Is ID mandatory in Hibernate?

Yes, hibernate requires an Id. Sometimes if you are dealing with a legacy database that for whatever reason does not have a key, you can define the key in Hibernate to be a composite key of all the columns for example, as this will be guaranteed to be unique.

Is it possible for an entity to not have a primary key?

Every entity in the data model must have a primary key whose values uniquely identify instances of the entity. To qualify as a primary key for an entity, an attribute must have the following properties: It must have a non-null value for each instance of the entity.

What is primary key in Hibernate?

Identifiers in Hibernate represent the primary key of an entity. This implies the values are unique so that they can identify a specific entity, that they aren't null and that they won't be modified. Hibernate provides a few different ways to define identifiers.


1 Answers

  1. Hibernate requires that entity tables have primary keys. End of story.
  2. 50k records is simply not that many when you're talking about a database.

My advice: add an autoincrement integer PK column to the table. You'll be surprised at how fast it is.

like image 179
Matt Ball Avatar answered Oct 30 '22 23:10

Matt Ball