Why is “public” not allowed for properties in JPA entities?

前端 未结 3 1831
悲哀的现实
悲哀的现实 2021-01-18 06:21

The JPA 2.0 specification says on page 22:

The instance variables of a class must be private, protected, or package visibility independent of whet

相关标签:
3条回答
  • 2021-01-18 06:41

    With public fields, there would be no way for proxying to work reliably -- if someone accesses a field directly, then there is no simple way for the persistence framework to intercept that call and (say) initialize the containing object, if it's a proxy.

    If field access is not possible, synthetic getter methods can be generated on the proxy object to intercept the method calls and do any required 'behind-the-scenes' work.

    0 讨论(0)
  • 2021-01-18 06:52

    To be sure that accessor methods are used (like get... set.... is.... ). Thats part of the isolation principle.

    0 讨论(0)
  • 2021-01-18 06:57

    With DataNucleus as JPA provider you could use public fields, but you would then need to mark any classes that access these fields as "persistence aware" (see the DataNucleus docs), so they can be enhanced to cater for this

    0 讨论(0)
提交回复
热议问题