openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Droscher <simon.drosc...@elasticpath.com>
Subject Re: Use getters/setters only
Date Thu, 11 Jun 2009 18:20:02 GMT

This section of the OpenJPA manual describes this:
http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/manual.html#jpa_overview_meta_field

The major point it makes is this:
When using property access, only the getter and setter method for a property
should ever access the underlying persistent field directly. Other methods,
including internal business methods in the persistent class, should go
through the getter and setter methods when manipulating persistent state.

Also, take care when adding business logic to your getter and setter
methods. Consider that they are invoked by the persistence implementation to
load and retrieve all persistent state; other side effects might not be
desirable.

The reason this point is stressed is that when using property access,
calling the getter and setter end up calling the enhanced versions of these
that OpenJPA creates during the bytecode enhancement. For example, when you
call a setter, OpenJPA will know to mark that field as dirty. When you call
the getter, OpenJPA may end up loading the relevant data from the DB (for
example on first access of a lazily loaded field within a transaction).

If you were accessing the field directly, OpenJPA won't know about it - so
in the above examples it may not mark the field as dirty or access the DB to
load the lazily loaded field value.

Note that none of this is relevant if you are using field access rather than
property access.


Daryl Stultz wrote:
> 
> I seem to remember somewhere reading that one should use getters and
> setters
> only when accessing persistent fields. This seems obvious from outside the
> entity class, but not so obvious from inside. Can anyone point me to the
> manual or other resource that describes this or explain it here?
> 

-- 
View this message in context: http://n2.nabble.com/Use-getters-setters-only-tp3062448p3063648.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message