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?
Asked
Active
Viewed 2,806 times
2
-
Its not mandatory in JDO, in that you can have tables without primary key (see "nondurable identity"), but is in JPA. – DataNucleus Jan 27 '13 at 08:45
2 Answers
3
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.

Hui Zheng
- 10,084
- 2
- 35
- 40
-
And the type of the ID can be any type ? I tried to find the documentation without success – Stefan Strooves Jan 27 '13 at 08:09
-
JPA poses some restrictions on ID type(e.g. primary key class must be serializable), you can read the specification from the link I gave for more details,(esp. chapter 2.4 which I mentioned in the answer). – Hui Zheng Jan 27 '13 at 08:17
3
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

ben75
- 29,217
- 10
- 88
- 134