Persistent field and Persistent properties - difference

G

gk

In JPA Entity classes , I don't understand what is a persistent field
and what is a persistent property ?


I have the following understanding as below

If the Entiry class has NO getter/setter then we call the instance
variable as persistent field
For example,
public class Contact implements Serializable{
protected String firstName;
// firstName is the persistent field.
// there is no getter/setter for firstName
.........................
............................



BUT

If the Entity class has getter/setter then we call the instance
variable as persistent property
For example,
public class Contact implements Serializable{
protected String firstName; // firstName is the persistent
property.
void setfirstName(String fname){firstName=fname;}
String setfirstName(String fname){return firstName;}
.........................
............................


Could you please let me know whether I have understood the terminology
persistent field and persistent property correctly. If not , please
let me know where is the difference between them ?
 
L

Lew

In JPA Entity classes , I don't understand what is a persistent field
and what is a persistent property ?


I have the following understanding as below

If the Entiry class has NO getter/setter then we call the instance
variable as persistent field
For example,
public class Contact implements Serializable{
protected String firstName;
// firstName is the persistent field.
// there is no getter/setter for firstName
.........................
...........................



BUT

If the Entity class has getter/setter then we call the instance
variable as persistent property
For example,
public class Contact implements Serializable{
protected String firstName; // firstName is the persistent
property.
void setfirstName(String fname){firstName=fname;}
String setfirstName(String fname){return firstName;}
.........................
...........................


Could you please let me know whether I have understood the terminology
persistent field and persistent property correctly. If not , please
let me know where is the difference between them ?

In this case they're synonymous.

Strictly speaking, a "field" is a member variable and a "property" is a
publicly accessible attribute, usually accessed in Java via get.../set...
methods, that behaves to the world like a field but might actually be
implemented differently.

That's a rather loose definition but works all right in practice.
 
L

Lew

In this case they're synonymous.

Strictly speaking, a "field" is a member variable and a "property" is a
publicly accessible attribute, usually accessed in Java via
get.../set... methods, that behaves to the world like a field but might
actually be implemented differently.

That's a rather loose definition but works all right in practice.

Another point - I don't think you can use JPA without getters and setters for
the entity fields.
 
G

gk

Another point - I don't think you can use JPA without getters and setters for
the entity fields.

Thanks for the post.

see this text below as to why I asked the differences between the two
wording...

The persistent state of an entity is represented either by its
*persistent fields or persistent properties*.

"persistent fields" vs "persistent properties" ... how they differ ?
 
T

Tom Anderson

Another point - I don't think you can use JPA without getters and setters for
the entity fields.

You can. You annotate the fields, and the provider accesses them with
reflection or some other mechanism. Some (including myself) consider it
preferable to access via the accessors, because it means you don't have to
have accessors where it's not appropriate (see discussions on getters,
setters and object orientation passim), and you're free to do clever stuff
in them when it is.

Thinking about it, i don't know if this is something the spec guarantees,
or something that implementations happen to support. I'll check.

tom
 
T

Tom Anderson

Thanks for the post.

see this text below as to why I asked the differences between the two
wording...

The persistent state of an entity is represented either by its
*persistent fields or persistent properties*.

"persistent fields" vs "persistent properties" ... how they differ ?

This looks like unnecessary and unhelpful splitting of hairs to me. After
all, it is *only* fields that are persisted, because fields are the only
place state lives. The distinction, if you must draw one, between fields
and properties is about how they're accessed, or even how they're thought
about.

tom
 
J

Jean-Baptiste Nizet

Le 12/10/2010 20:32, Tom Anderson a écrit :
You can. You annotate the fields, and the provider accesses them with
reflection or some other mechanism. Some (including myself) consider it
preferable to access via the accessors, because it means you don't have
to have accessors where it's not appropriate (see discussions on
getters, setters and object orientation passim), and you're free to do
clever stuff in them when it is.

Thinking about it, i don't know if this is something the spec
guarantees, or something that implementations happen to support. I'll
check.

It's guaranteed by the spec :

2.1.1 Persistent Fields and Properties
The persistent state of an entity is accessed by the persistence
provider runtime[1] either via JavaBeans
style property accessors or via instance variables. A single access type
(field or property access) applies
to an entity hierarchy. When annotations are used, the placement of the
mapping annotations on either
the persistent fields or persistent properties of the entity class
specifies the access type as being either
field- or property-based access respectively.


JB.
 
T

Tom Anderson

Le 12/10/2010 20:32, Tom Anderson a ?crit :
You can. You annotate the fields, and the provider accesses them with
reflection or some other mechanism. Some (including myself) consider it
preferable to access via the accessors, because it means you don't have
to have accessors where it's not appropriate (see discussions on
getters, setters and object orientation passim), and you're free to do
clever stuff in them when it is.

Thinking about it, i don't know if this is something the spec
guarantees, or something that implementations happen to support. I'll
check.

It's guaranteed by the spec :

2.1.1 Persistent Fields and Properties
The persistent state of an entity is accessed by the persistence
provider runtime[1] either via JavaBeans style property accessors or via
instance variables. A single access type (field or property access)
applies to an entity hierarchy. When annotations are used, the placement
of the mapping annotations on either the persistent fields or persistent
properties of the entity class specifies the access type as being either
field- or property-based access respectively.

Je suis redevable a votre bienveillance. And needless to say, i am no
longer going to check!

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top