Access property usage for the <property> element:
Access has two optional values:
1,property: This is the default value, which indicates that hibernate accesses the properties of the class through the set and get methods. This is the preferred method of recommendation. You must set the get and set methods for this property to be accessed by hibernate. Otherwise it will be an error.
2,field: Indicates that hibernate accesses the properties of the class directly through the reflection mechanism of java. You can not set the get and set properties for this property.
This property works well when we piece together a user name. The following example:
Java code
- Public class Customer implements Serializable {
- /**
- *
- */
- private Static final long serialversionuid = 1L;
- Private String FirstName;
- private String LastName;
- Public String GetName () {
- return firstname + "" + LastName;
- }
- public void SetName (String name) {
- StringTokenizer t = new StringTokenizer (name);
- FirstName = T.nexttoken ();
- LastName = T.nexttoken ();
- }
- }
There is no need to configure the FirstName and LastName properties in the persistence mapping file for this class, but to map the Name property, as we end up with the full name.
Java code
- <property name="name" column="name"/>
Although the name attribute is not defined in the customer class, hibernate does not directly access the Name property, but is accessed through SetName and GetName. Regardless of whether the name attribute exists in the customer class, as long as the name attribute is mapped in the mapping file, HQL can access it, whereas the FirstName and LastName properties do not have a mapping in the mapping file, so the two properties cannot be accessed in HQL.
Hibernate access policy for accessing persisted class properties