@Transient marks a field or property of a JPA entity as not persistent. The persistence provider ignores it when it creates tables, saves, updates or loads rows, so the value lives only in the Java object. Use it for calculated values, caches and temporary state that must not become database columns.
The annotation is jakarta.persistence.Transient in Jakarta Persistence 3.x (Spring Boot 3 and later, Hibernate 6). Older projects on Spring Boot 2 import javax.persistence.Transient instead.
By default every non-static, non-transient field of an entity is mapped to a column. Adding @Transient removes the attribute from the mapping:
The table for Person has columns for id, first_name, last_name and birth_date. There is no display_name column. When Hibernate loads a Person, displayName is null until your code sets it.
The Java transient modifier also excludes a field from JPA persistence, in addition to excluding it from Java serialisation:
The two mechanisms are equivalent for JPA. The difference is that the keyword also affects java.io.Serializable behaviour, while @Transient affects only the persistence mapping. Use the annotation when you want the intent to be explicit and the keyword when you also want to skip Java serialisation.
The access type decides where annotations go. With field access (the @Id is on a field), annotate fields. With property access (the @Id is on a getter), annotate the getter:
Without @Transient on getGross(), a provider using property access would try to map a gross attribute and then fail because there is no setter. With field access, a derived getter such as getAge() in the earlier example does not need the annotation, because only fields are mapped.
displayName fails at startup or at run time.@PostLoad callback.UPDATE.@Transient has no effect on JSON output. Jackson serialises the field if it has a getter. Use @JsonIgnore or a separate response DTO to control the JSON shape.If you want the database to compute a value, @Transient is the wrong tool. Hibernate offers @Formula, which embeds a SQL expression and loads the result with the entity:
@Formula is Hibernate-specific, read-only and calculated at load time. For provider-neutral code, use a database view mapped as an entity or a projection query.
Spring Data has its own org.springframework.data.annotation.Transient for stores that do not implement JPA, such as MongoDB. In a JPA entity, use the Jakarta annotation. Importing the wrong Transient is a frequent mistake and the compiler will not warn you, so check the import line if a column still appears.
It prevents the column from being generated, so schema creation from entities will not include it. If the column already exists in a hand-written schema, JPA simply ignores it.
For JPA they behave the same. The Java keyword also stops Java serialisation, while the annotation only affects persistence.
No. Queries operate on persistent attributes. Make the attribute persistent, use @Formula, or filter in Java after loading.
JPA does not populate it. Compute it in a getter, in a @PostLoad method, or in your service layer.
@Transient keeps a field or property out of the JPA mapping, so it is never written to or read from the database. Use it for calculated and temporary values, remember that it cannot be queried and is not restored on load, and import jakarta.persistence.Transient on current stacks.