Why are "public" not allowed in JPA object properties?

JPA 2.0 specification on page 22:

Class instance variables must be private, protected, or package visibility, regardless of whether access to fields or access to resources is used. When access to properties is used, access methods to resources must be public or secure.

Why is sharing not allowed?

+4
source share
3 answers

It was impossible to reliably proxy work in open fields - if someone accesses the field directly, then there is no easy way to intercept this call and (for example) initialize the containing object, if it is a proxy.

If access to the field is unavailable, synthetic getter methods can be created on the proxy object to intercept method calls and perform any required β€œoff-screen” work.

+8
source

With DataNucleus as a JPA provider, you can use public fields , but then you will need to mark any classes that access these fields as "persistence aware" (see DataNucleus docs), so they can be reinforced to satisfy this

+3
source

To ensure that access methods are used (for example, get ... set .... is ....). This is part of the principle of isolation.

0
source

All Articles