Why do Entity classes must have an @Id?

Viewed 2545

I am a little confused about that. When I try to create an entity that without @Id, the compile-time gives me an error that I must set primary key use @Id, and I even can't deploy the application to the application server (I use payara). But the requirement for entity classes in the official Java EE 7 tutorial documentation doesn't contain the rule that an entity must have @Id. And, the definition of an entity in that documentation:

Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table.

But I can create a data-table that without primary key.

So my question is actually that why do we must set @Id in an entity. Are there any other considerations? For example, use the @Id to identify a specific entity in persistence context, so that we can use em.find(id)? And what should I do if I insist on creating an entity without @Id, lol.

Thanks for your help.

3 Answers

If I talk about database, every table should have some column (or columns) that can uniquely identifies a particular row.

In a same manner, as our entity class represents a table, so @Id is immensely required there.

(My answer is just a hint for those, who are beginners.)

Related