Attributes that are part of a persistent entity but not intended to be persistent can either be modified
with the transientmodifier in Java or be annotated with the @Transientannotation. If either is
specified, the provider runtime will not apply its default mapping rules to the attribute on which it was
specified.
Transient fields are used for various reasons. One might be the case earlier on in the chapter
when we mixed the access mode and didn’t want to persist the same state twice. Another might be
when you want to cache some in-memory state that you don’t want to have to recompute, rediscover,
or reinitialize. For example, in Listing 4-13 we are using a transient field to save the correct localespecific word for Employeeso that we print it correctly wherever it is being displayed. We have used the
transientmodifier instead of the @Transientannotation so that if the Employeegets serialized from one
VM to another then the translated name will get reinitialized to correspond to the locale of the new
VM. In cases where the non-persistent value should be retained across serialization, the annotation
should be used instead of the modifier.
đang được dịch, vui lòng đợi..
