But reading page 231 of EJB in Action they recommend that by using field-based-access that you mark your fields private and expose these fields by a getter/setter method. This means that it's possible to mark your fields private with a field-based-access.
Deepika Joshi wrote:
I got a doubt, when user uses field-based, then do we need to declare field with public access?
The access type determines how the persistence provider accesses persistent state: See JPA spec 2.1.1:
If the entity has field-based access, the persistence provider runtime accesses instance variables directly.
If the entity has property-based access, the persistence provider runtime accesses persistent state via the property accessor methods.
The entity's variables must not be public and should be accessed by the user via getter/setter methods: See JPA spec 2.1:
An instance variable may be directly accessed only from within the methods of the entity by the entity instance itself. Instance variables must not be accessed by clients of the entity. The state of the entity is available to clients only through the entity's accessor methods (getter/setter methods) or by other business methods. Instance variables must be private, protected, or package visibility.