why the jpa annotations are applied on field or on getter methods. if i try to apply the annotations on setter method then compiler generate the error. because compiler ignore the annotation on setter method. what is the reason behind them?
This is is how it's specified. Per JPA Specification:
- When field-based access is used, the object/relational mapping annotations for the entity class annotate the instance variables, and the persistence provider runtime accesses instance variables directly. All non-transient instance variables that are not annotated with the Transient annotation are persistent.
- When property-based access is used, the object/relational mapping annotations for the entity class annotate the getter property accessors[7], and the persistence provider runtime accesses persistent state via the property accessor methods. All properties not annotated with the Transient annotation are persistent.
- Mapping annotations must not be applied to fields or properties that are transient or Transient.
You have two options. Either use field level annotation or property (getter method) annotation. There is no third option.