background image

Persistent Properties

<< Persistent Fields and Properties | Primary Keys in Entities >>
<< Persistent Fields and Properties | Primary Keys in Entities >>

Persistent Properties

Persistent Fields
If the entity class uses persistent fields, the Persistence runtime accesses entity class instance
variables directly. All fields not annotated javax.persistence.Transient or not marked as
Java transient will be persisted to the data store. The object/relational mapping annotations
must be applied to the instance variables.
Persistent Properties
If the entity uses persistent properties, the entity must follow the method conventions of
JavaBeans components. JavaBeans-style properties use getter and setter methods that are
typically named after the entity class's instance variable names. For every persistent property
property of type Type of the entity, there is a getter method getProperty and setter method
set
Property. If the property is a boolean, you may use isProperty instead of getProperty. For
example, if a Customer entity uses persistent properties, and has a private instance variable
called firstName, the class defines a getFirstName and setFirstName method for retrieving
and setting the state of the firstName instance variable.
The method signature for single-valued persistent properties are as follows:
Type getProperty()
void setProperty(Type type)
Collection-valued persistent fields and properties must use the supported Java collection
interfaces regardless of whether the entity uses persistent fields or properties. The following
collection interfaces may be used:
java.util.Collection
java.util.Set
java.util.List
java.util.Map
If the entity class uses persistent fields, the type in the above method signatures must be one of
these collection types. Generic variants of these collection types may also be used. For example,
if the Customer entity has a persistent property that contains a set of phone numbers, it would
have the following methods:
Set<PhoneNumber> getPhoneNumbers() {}
void setPhoneNumbers(Set<PhoneNumber>) {}
The object/relational mapping annotations for must be applied to the getter methods. Mapping
annotations cannot be applied to fields or properties annotated @Transient or marked
transient
.
Entities
Chapter 24 · Introduction to the Java Persistence API
687