By default the access type is defined by the place where you put your mapping annotations. If you put them on the field - it will be AccessType.FIELD
, if you put them on the getters - it will be AccessType.PROPERTY
.
Sometimes you might want to annotate not fields but properties (e.g. because you want to have some arbitrary logic in the getter or because you prefer it that way.) In such situation you must define a getter and annotate it as AccessType.PROPERTY
.
As far as I remember, if you specify either AccessType.FIELD
or AccessType.PROPERTY
on any of the entity fields / methods you must specify the default behaviour for the whole class. And that's why you need to have AccessType.FIELD
on the class level (despite that AccessType.FIELD
is the default value.)
Now, if you wouldn't have @Transient
on the phnnumber
field, the JPA would provide you with a 3 columns table:
- id,
- phnnumber,
- getphnnumber.
That's because it would use AccessType.FIELD
for all of the entity fields (id
and phnnumber
) and, at the same time, it'd use AccessType.PROPERTY
for your getter (getPhnnumber()
).
You'll end with phone number mapped twice in the database.
Therefore, the @Transient
annotation is required - it means that the entity won't store the value of the field in the underlying storage but the value returned by your getter.