liveBook · Manning

@transient

As explained in Spring in Action, Sixth Edition

@Transient is a Spring Data MongoDB annotation that excludes a domain property from document persistence.

Use @Transient on a domain property that should remain available in an object but should not be persisted. In Spring Data MongoDB, properties normally map to document fields with the same names unless @Field or @Transient specifies otherwise. section 4.2.2

The same annotation solves a practical problem with Spring Data R2DBC: marking a collection @Transient prevents R2DBC from trying to persist a relationship it does not support. In a TacoOrder, the tacos collection can hold Taco objects while tacoIds is written to the database. section 13.1.4

Listing 13.8 Adding a Taco collection to TacoOrder
@Data
public class TacoOrder {
 
  ...
  
  @Transient
  private transient List<Taco> tacos = new ArrayList<>();
 
  public void addTaco(Taco taco) {
    tacos.add(taco);
    if (taco.getId() != null) {
      tacoIds.add(taco.getId());
    }
  }
 
}

The TacoOrder listing puts @Transient on the tacos collection, alongside Java’s transient keyword. The addTaco() method keeps the collection and the persisted ID set in step. section 13.1.4

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines the mapping effect and works through an R2DBC relationship that would otherwise cause an error.
Examples
Uses a TacoOrder collection that holds Taco objects while their IDs are persisted.
What you can do after
Apply it to keep an R2DBC order collection available in memory without asking R2DBC to persist that relationship.
How it connects
Connects property mapping to @Field, MongoDB documents, R2DBC relationships, and aggregate roots.

Also explained in

Compare how 2 books explain @transient →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.