You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Daryl Stultz <da...@6degrees.com> on 2009/06/11 16:54:20 UTC

Use getters/setters only

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?

Thanks.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Use getters/setters only

Posted by Daryl Stultz <da...@6degrees.com>.
On Tue, Jun 16, 2009 at 9:55 AM, Craig L Russell <Cr...@sun.com>wrote:

> For me, property access should only be used


Thanks to Craig and Michael.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Use getters/setters only

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Daryl,

On Jun 11, 2009, at 12:24 PM, Daryl Stultz wrote:

> I am using field access. I placed my annotations on the fields simply
> because I like them there. I didn't realize there was a functional
> difference. Is there any advantage/disadvantage to field vs property  
> access?
> It seems property access has the potential gotcha, while field does  
> not.
> Perhaps there is some other cost...

For me, property access should only be used with persistent abstract  
classes or persistent interfaces. In these cases, the properties are  
implemented by OpenJPA and the user has no opportunity for mischief.

In the case of non-abstract classes, IMHO using properties mixes two  
concerns: the client view (which has to use the accessors to get to  
the data) and the persistence implementation view (which has to use  
the accessors to store the data in the database). When using property  
access for non-abstract classes, you can't put business logic into the  
accessors because this interferes with the persistence  
implementation's use of the accessors.

HTH,

Craig

Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: Use getters/setters only

Posted by Michael Dick <mi...@gmail.com>.
Minor clarification, the client of the entity (ie your application) should
not access the fields directly - regardless of the access type.

>From section 2.1 of the JPA 1.0 spec

The persistent state of an entity is represented by instance variables,
which may correspond to Java-
Beans properties. 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 other business methods. Instance variables must be private, protected, or
package visibility.

Regarding advantages and disadvantages of different access types it really
depends on the application. The reason (AFAIK) for the difference in
behavior is to allow for business logic in the accessor methods. When you
have business logic there it may make sense for the JPA provider to use
FIELD access (and avoid your logic). If there's no business logic then
PROPERTY may be the way to go.

hth,
-mike

On Thu, Jun 11, 2009 at 2:24 PM, Daryl Stultz <da...@6degrees.com> wrote:

> On Thu, Jun 11, 2009 at 2:20 PM, Simon Droscher <
> simon.droscher@elasticpath.com> wrote:
>
> >
> > 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
>
>
> That's exactly what I was looking for, thanks.
>
> Note that none of this is relevant if you are using field access rather
> than
> > property access.
> >
>
> But I didn't expect that!
> (from the manual)
>
> > To use field access for an entity using annotation metadata, simply place
> >> your metadata and mapping annotations on your field declarations:
> >>
> >
> I am using field access. I placed my annotations on the fields simply
> because I like them there. I didn't realize there was a functional
> difference. Is there any advantage/disadvantage to field vs property
> access?
> It seems property access has the potential gotcha, while field does not.
> Perhaps there is some other cost...
>
> --
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
>

Re: Use getters/setters only

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Jun 11, 2009 at 2:20 PM, Simon Droscher <
simon.droscher@elasticpath.com> wrote:

>
> 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


That's exactly what I was looking for, thanks.

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

But I didn't expect that!
(from the manual)

> To use field access for an entity using annotation metadata, simply place
>> your metadata and mapping annotations on your field declarations:
>>
>
I am using field access. I placed my annotations on the fields simply
because I like them there. I didn't realize there was a functional
difference. Is there any advantage/disadvantage to field vs property access?
It seems property access has the potential gotcha, while field does not.
Perhaps there is some other cost...

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Use getters/setters only

Posted by Simon Droscher <si...@elasticpath.com>.
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.