ID annotation
The @Id annotation offer the simplest mechanism to define
the mapping to the primary key.
You can associate the @Id annotation to fields/properties of these
types:
- Primitive Java™ types and their wrapper classes
- Arrays of primitive or wrapper types
- Strings: java.lang.String
- Large numeric types: java.math.BigDecimal, java.math.BigInteger
- Temporal types: java.util.Date, java.sql.Date
We discourage the usage of floating point types (float and double, and their wrapper classes) for decimal data, because you can have rounding errors and the result of equals operator is unreliable in these cases. Use BigDecimal instead.
The @Id annotation fits well in scenarios where a natural primary key is available, or when database designers use surrogate primary keys (typically an integer) that has no descriptive value and is not derived from any application data in the database. On the other end, composite keys are useful when the primary key of the corresponding database table consists of more than one column. Composite keys can be defined by the @IdClass or @EmbeddedId annotation.