Java Reference
In-Depth Information
@Access(Accesss.TypeFIELD)
private double creditWorth;
@Transient
public double getCreditWorth() { return creditWorth; }
public void setCreditWorth(double cw) {
creditWorth = (cw <= 0) ? 50.0 : cw;
}
}
The @Access annotation is a nice addition to JPA so that all the objects in your domain
model hierarchy don't need to have either field- or property-based access. The @Access
annotation allows for mixing the two access types. This is especially nice if objects in the
domain model hierarchy are part of shared projects that can't be easily changed.
These examples introduced the @Transient annotation. This is another commonly used
annotation in JPA, and it prevents JPA from managing whatever it annotates. We'll discuss
the @Transient annotation in more detail next.
Defining a transient field
Because JPA will automatically attempt to persist a Java object property with standard
JavaBeans getter and setter methods, is it possible to have a property in your object that JPA
won't persist? The answer is yes. JPA follows the serialization model and has a @Tran-
sient annotation. The @ Transient annotation is used to tell JPA to ignore a property
completely. If a property is marked with @ Transient , JPA won't try to select, insert, or
update the property's value. Suppose you add the birthday and age properties to the
User object as follows:
public class User {
@Column(name="DATE_OF_BIRTH")
Date birthday;
@Transient
int age;
//...Other getters and setters ommitted for brevity
public void setBirthday(Date birthday) {
this.birthday = birthday;
// calculate age...
}
}
Search WWH ::




Custom Search