Stefan Strooves
Stefan Strooves

Reputation: 634

JPA tables are ID is mandatory?

I saw that the ID type is defined in every JPA table. Is it mandatory? Or is there any option that I can get class without the ID member?

Upvotes: 2

Views: 4159

Answers (2)

Hui Zheng
Hui Zheng

Reputation: 10224

Id(i.e. primary key) is mandatory in JPA. As JSR317(Java Persistence API, which could be downloaded here) chapter 2.4 said(first sentence):

Every entity must have a primary key

BTW, besides Id annotation, one can also use EmbeddedId annotation for composite primary keys.

Upvotes: 3

ben75
ben75

Reputation: 28726

Id is required by JPA, but it is not required that the Id specified in your mapping match the Id in your database.

For instance you can map a table with no id to a jpa entity. To do it just specify that the "Jpa Id" is the combination of all columns.

Note that for performence reason, it's important to have a good index on column(s) specified as Id in Jpa

Upvotes: 3

Related Questions