Java Reference
In-Depth Information
The
@Access
annotation must first be defined at the class level to set the default access
type for the entire entity. The access types may be either
AccessType.FIELD
or
Ac-
cessType.PROPERTY
. Here's an example of setting the
User
entity access type to
FIELD
:
@Entity
@Access(FIELD)
public class User { ... }
You can now define the
Seller
entity and do some special things:
@Entity
@Access(FIELD)
public class Seller extends User {
//...the rest of seller omitted for brevity
@Transient
private double creditWorth;
@Column(name="CREDIT_WORTH")
@Access(AccessType.PROPERTY)
public double getCreditWorth() { return creditWorth; }
public void setCreditWorth(double cw) {
creditWorth = (cw <= 0) ? 50.0 : cw;
}
}
Notice that the
Seller
entity uses the
@Access
annotation to set
FIELD
as the default
for the class. But it uses
@Transient
and
@Access
to override field-based access for
creditWorth
in favor of property-based access. Everything else in
Seller
will be
field-based access except for
creditWorth
. When performing an override like this, you
always use
@Transient
and
@Access
together. This prevents JPA from basically try-
ing to map
creditWorth
twice.
In the preceding example the default for
Seller
was field-based access and you per-
formed an override for
creditWorth
to be property-based access. But this could just as
easily have been the other way around. The default for
Seller
can be
PROPERTY
and
the override for
creditWorth
can make it field-based access. Here's what that would
look like:
@Entity
@Access(PROPERTY)
public class Seller extends User {
//...the rest of seller omitted for brevity
@Column(name="CREDIT_WORTH")