The @Transient annotation tells JPA that a field or property of an entity is not persistent, so it is never written to or read from the database. Without it, JPA maps every non-static, non-transient field of an entity to a column. Use @Transient for values that are calculated at runtime, cached, or only meaningful in memory.
In current versions (Jakarta Persistence 3.x, used by Spring Boot 3 and later) the annotation is jakarta.persistence.Transient. Older code uses javax.persistence.Transient, which has the same behaviour.
By default, JPA treats each field of an @Entity class as persistent and maps it to a column named after the field. Marking a field with @Transient removes it from the mapping:
When you persist or update an Employee, the provider ignores bonus. When you load one, bonus is null because nothing populates it. Schema generation also skips the field, so there is no column to create.
A value that can be computed from persistent fields should not be stored twice:
With property access, the annotation goes on the getter. With field access (the usual choice when @Id is on a field), put it on the field, and a computed getter without a backing field needs no annotation at all.
Flags such as "dirty in the UI", a lazily computed hash, or a reference to a service object belong to the running application, not the database row. Mark them @Transient.
Plain-text passwords entered in a form, one-time tokens and similar sensitive values can be held in a transient field and hashed into a persistent field before saving. Keep in mind that @Transient only affects JPA: the field is still serialised by Jackson unless you also exclude it there.
Because a transient field is empty after loading, you can fill it with a lifecycle callback:
@PostLoad runs after the entity is read from the database, so the transient field is ready before your code uses it.
Java has its own transient modifier, meant for serialisation. JPA treats both as non-persistent, so either works for exclusion, but they differ in other behaviour:
JPA @Transient | Java transient | |
|---|---|---|
| Excluded from the database mapping | Yes | Yes |
| Excluded from Java serialisation | No | Yes |
| Intent is explicit to readers | Yes | Mixed with serialisation meaning |
Use @Transient when your intent is database mapping, and the keyword when you also want the field skipped by java.io.Serializable. Use both only when you need both effects.
@Transient fields in JPQL or Criteria queries. The provider does not know them, so a query referencing one fails.refresh(), find() in a new persistence context, or a detached entity being merged, the transient value is lost or ignored.org.springframework.data.annotation.Transient, and importing the wrong one silently leaves the field mapped.@Id sits.jakarta.persistence imports on Spring Boot 3 and later. The javax.persistence package is not on the classpath there.JPA expects a column with that name. Schema validation fails or a SQL error occurs on the first query, because the column does not exist.
Yes, it removes the association from the mapping. This is occasionally useful for a convenience reference, but a regular persistent association is normally the better design.
No. @Formula is Hibernate specific and fills a read-only property from a SQL expression at load time. @Transient just excludes the field.
Records cannot be JPA entities, because entities need a no-argument constructor and mutable state. Jakarta Persistence 3.2 allows records as embeddables. For entities, use a normal class.
@Transient excludes a field or property from JPA persistence. Use it for derived values, runtime state and sensitive temporary data, populate it with @PostLoad when needed, and import jakarta.persistence.Transient on modern stacks. Remember it does not affect serialisation or JSON output.