Could you please explain how names of the field will not correspond to method names (will not correspond to JavaBean conventions).If a property based access is chosen for an entity class, the names of the fields do not have to correspond to the method names.
About Question enthuware.oce-jpad.v6.2.559 :
Moderator: admin
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
About Question enthuware.oce-jpad.v6.2.559 :
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
Code: Select all
private int xxx;
public int getA(){
return xxx;
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
field is "name"
setter and getter do not have to be "getName" and "setName"?
I think Javabeans would require exact naming as above, but JPA specifications are not required to be compliant. Correct?
setter and getter do not have to be "getName" and "setName"?
I think Javabeans would require exact naming as above, but JPA specifications are not required to be compliant. Correct?
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
Correct.johnlong wrote:field is "name"
setter and getter do not have to be "getName" and "setName"?
Incorrect. Javabeans has no such rule on the naming of the field.I think Javabeans would require exact naming as above, but JPA specifications are not required to be compliant. Correct?
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
You mean it is convention, but it is not recommendation?
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
Javabeans conventions are conventions. And they don't impose any constraint on the name of the variable. So no, it is not an official convention. It is, however, considered a good programming practice (or recommendation, if you want to call it that:) ) to keep your variable name in line with the getter/setter names to avoid confusion.
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
JPA Specification 2.1 (Section 2.2) says :
It is required that the entity class follow the method signature conventions for JavaBeans read/write properties (as defined by the JavaBeans Introspector class) for persistent properties when property access is used.
In this case, for every persistent property property of type T of the entity, there is a getter method, getProperty, and setter method setProperty. For boolean properties, isProperty may be used as an alternative name for the getter method.
For single-valued persistent properties, these method signatures are:
• T getProperty()
• void setProperty(T t)
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
Not sure what is your point but nowhere does it talk about the field/variable name.
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
What about this sentence :
In this case, for every persistent property property of type T of the entity, there is a getter method, getProperty, and setter method setProperty.
In this case, for every persistent property property of type T of the entity, there is a getter method, getProperty, and setter method setProperty.
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
Yes, but property != variable/field.
As soon as a class gets a getX (and setX, if it is a mutable property), it is said to have a property named x. Irrespective of whether it has a variable/field named x or not.
As soon as a class gets a getX (and setX, if it is a mutable property), it is said to have a property named x. Irrespective of whether it has a variable/field named x or not.
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
If property is not field, then what is it?
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
See the first answer here: http://stackoverflow.com/questions/1011 ... ty-in-java
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
Coming back to this discussion after almost 4 years
and again :
JPA Specification 2.1 (Section 2.2) says :
And you provided code example
Since Entity is not strictly (always) immutable object, and in that case the mutator (setYYY method) will be present, therefore xxx above is not (always) a field , it is a property.
Therefore, I believe the answer to this question is incorrect. Specifications clearly state that JavaBean conventions should be followed for property-based access.
First of all, by definition the statement itself is contradictory - if you say property access it means that there are no fields in the Entity class, because "fields" are technically properties (promoted to properties from fields if you will by virtue of property accessor or/and mutator existense).If a property based access is chosen for an entity class, the names of the fields do not have to correspond to the method names.
and again :
JPA Specification 2.1 (Section 2.2) says :
And you are sayingIt is required that the entity class follow the method signature conventions for JavaBeans read/write properties (as defined by the JavaBeans Introspector class) for persistent properties when property access is used.
In this case, for every persistent property property of type T of the entity, there is a getter method, getProperty, and setter method setProperty. For boolean properties, isProperty may be used as an alternative name for the getter method.
For single-valued persistent properties, these method signatures are:
• T getProperty()
• void setProperty(T t)
and referring to Oracle glossary https://docs.oracle.com/javase/tutorial ... ary.html#Pproperty != variable/field.
Since you are referring to this definition, note it does not say here HOW 'characteristic of an object is set', i.e. as long as it is being set by user it is a property.property
Characteristics of an object that users can set, such as the color of a window.
And you provided code example
Code: Select all
private int xxx;
public int getA(){
return xxx;
}
Therefore, I believe the answer to this question is incorrect. Specifications clearly state that JavaBean conventions should be followed for property-based access.
-
- Site Admin
- Posts: 10326
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.oce-jpad.v6.2.559 :
No, you are misintepreting.
>if you say property access it means that there are no fields in the Entity class
That's not what the explanation means. Property access simply means using the getter and setter methods. It does not mean there is no field. There may or may not be a field by the same name.
Yes, JavaBeans conventions for property are to be followed and they are being followed in this question and the explanation. Please go through it again.
>if you say property access it means that there are no fields in the Entity class
That's not what the explanation means. Property access simply means using the getter and setter methods. It does not mean there is no field. There may or may not be a field by the same name.
Yes, JavaBeans conventions for property are to be followed and they are being followed in this question and the explanation. Please go through it again.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 3 guests