0

I am having a difficult time trying to understand the Transient annotation of JPA. I assume the fields noted with Transient annotation will be stored in a local cache and not persisted in DB. I basically like to know when will it be cleaned up from the local cache? I am using this for a table to store its intermittent status and I use this value in a method that is returned after I call an external service. Is this an appropriate use case? If so, what will be the life time of such a transient field?

Jacek Cz
  • 1,872
  • 1
  • 15
  • 22
user2220232
  • 71
  • 1
  • 8

1 Answers1

-1
@Entity
class Sample {
  @Transient 
  String fieldOne;

  transient String otherField;
}

fieldOne is not transient (has not transient keyword), so is serialised (to/from cache, network, file or other sources). But JPA will not store it in database, because annotation denies.

otherField is not seriazable, has transient keyword (i.e. after getting from cache engine, or network can/will be null), but is pesissted in JPA database with default behaviour

This is not academic discussion, sometimes it is useable. Usually values computed from others, or hashed /encrypted /hidden fields.

Jacek Cz
  • 1,872
  • 1
  • 15
  • 22
  • Thanks for the response. In this example, what will be the lifetime of fieldOne once it is assigned a value? – user2220232 Jan 05 '18 at 21:02
  • 1
    this is "normal" Java field, notching change, and "only" not stored in database. Annotation has meaning only (i hope) for JPA framework. So when in the futore new instance will be born by reanging from bd, will be null – Jacek Cz Jan 05 '18 at 21:17
  • 1
    This answer is simply wrong. JPA will **not** persist fields that are marked `transient` either. For anyone reading this question, see https://stackoverflow.com/questions/2154622/why-does-jpa-have-a-transient-annotation for a more detailed explanation – crizzis Jan 12 '18 at 16:37