Manuel Drieschmanns
Manuel Drieschmanns

Reputation: 734

Transient fields are not null after loading

I'm confused by a surprising behaviour of eclipselink 2.5.2. In same cases the transient fields of an entity are set with the last value after loading the object. From my point of view there is no definition how the state of transient fields should be after loading (see https://download.oracle.com/otn-pub/jcp/persistence-2.0-fr-oth-JSpec/persistence-2_0-final-spec.pdf?AuthParam=1551111289_d4f5a797aa325dac1adb64fb8b75c2af). Can anyone explain these behaviour? I prevend the behaviour and set all @Transient fields within a @PostLoad annotated method to null.

@Entity
@Cacheable(true)
@Cache(expiry = 300000)
@Table(uniqueConstraints=@UniqueConstraint(name = "uc_b_a_v", columnNames = {"a_id", "v"}))
public class Entity {

    @Transient
    private String transientfield;  

    @PostLoad
    public void onPostLoad() {
        transientfield = null;
    }
}

Upvotes: 0

Views: 428

Answers (1)

Manuel Drieschmanns
Manuel Drieschmanns

Reputation: 734

This post from the eclipselink forum explains the behaviour very well: https://www.eclipse.org/forums/index.php/t/206082/

EclipseLink enables a shared cache by default. This shared cache can either support maintaining transient variables, or not.

If weaving is used, the shared cache objects are cloned to/from the persistence context, thus preserving the state of transient variables.

If weaving is not used, then new instances are created in the persistence context, and the non-transient state (only) is copied from the shared object.

So, if you don't want transients cached, then you can disable internal weaving ("eclipselink.weaving.internal"="false"), this will still allow weaving for LAZY and change tracking.

You could also disable the shared cache, or disable weaving entirely, or configure the CopyPolicy of your Entity, or use the DescriptorEvent postClone/postMerge.

Upvotes: 1

Related Questions