Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA: Why the annotations are applied on getter or field

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?

like image 637
Harmeet Singh Taara Avatar asked Dec 25 '12 06:12

Harmeet Singh Taara


1 Answers

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.

like image 70
Yogendra Singh Avatar answered Nov 15 '22 01:11

Yogendra Singh