0
votes

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;
    }
}
1
You may need to clear the cache: wiki.eclipse.org/EclipseLink/Examples/JPA/Caching - Alan Hay
PostLoad is only called when an entity is loaded from the database. If the entity is from the current cache, it is returned as is. - Chris

1 Answers

1
votes

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.