Using @Transient in Spring boot + Examples

In this tutorial you will see how to use @Transient annotation in Spring Boot for data persistence and also the difference between javax.persistence.Transient and org.springframework.data.annotation.Transient annotations along with some examples.

1. What is @Transient annotation in Spring?

@Transient annotation is used to mark a field to be transient for the mapping framework, which means the field marked with @Transient is ignored by mapping framework and the field not mapped to any database column (in RDBMS) or Document property (in NOSQL). Thus the property will not be persisted to data store. @Transient exists in org.springframework.data.annotation package. The mapping framework will be different for each Spring Data module (Spring Data Jdbc, Spring Data MongoDB, Spring Data JPA .. etc).

Following is sample snippet for mapping @Transient in Spring Boot Data JDBC.

@Data // lombok
public class User {

	@Id
	private Long id;
	private String userName;
        //@JsonIgnore - to not expose to view (recommended to use in Dto but not in entity class)
	private String password;
	private Date createdTime;
	private Date updatedTime;
	@Column("DOB") // to map db column if property not same as column name
	private Date dateofBirth;
	private UserType userType;

	@org.springframework.data.annotation.Transient // to not persist into DB (just to expose to view/client)
	private String dateOfBirthString;
	
	// to display on view
	public String getDateOfBirthString() {
		return this.dateofBirth.toString();
	}
}

Following is sample snippet for mapping @Transient in Spring Boot Data MongoDB.

@Document
@Data // lombok
public class User {

	@Id
	private String id;
	private String userName;
	private static String password;
	private Date createdTime;
	private Date updatedTime;
	private Date dateofBirth;
	private UserType userType;

	@org.springframework.data.annotation.Transient
	private String dateOfBirthString;
	
	// to display on view
	public String getDateOfBirthString() {
		return this.dateofBirth.toString();
	}

}

2. What is @Transient in javax.persistence?

If you are working with Spring Boot Data JPA for data persistence, JPA also provides javax.persistence.Transient annotation, and which is also used to ignore a field to not to be persisted in database.

Following is sample snippet for mapping @Transient in Spring Boot Data JPA

@Entity(name = "USER")
public class User {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
	@GenericGenerator(name = "native", strategy = "native")
	@Column(name = "ID")
	private Long id;
	private String userName;
	//@JsonIgnore - to not expose to view (recommended to use in Dto but not in entity class)
	private String password;
	private Date createdTime;
	private Date updatedTime;
	@Column(name = "DOB")
	private Date dateofBirth;
	@Enumerated(value = EnumType.STRING)
	private UserType userType;
	@javax.persistence.Transient // to not persist into DB (just to expose to view or client/intermediate operations)
	private static String dateOfBirthString;

       // Getter and Setter

}

3. JPA @Transient vs Spring @Transient

  1. Either JPA or Spring @Transient is used to not to be persist a field in data store. But if you use org.springframework.data.annotation.Transient in Spring Data JPA / Spring Boot Data JPA application it doesn’t take any effect. Because the underlying Object relational mapping done through JPA implementation (Hibernate by default). In Spring with JPA application always prefer to use javax.persistence.Transient.
  2. If you are working with Spring Data / Spring Boot Data modules other than Data JPA (for example Spring Data JDBC, Spring Data MongoDB, Spring Data Redis ..etc) org.springframework.data.annotation.Transient should be used to ignore fields to not persist in data store.
  3. If you use JPA, the fields declared with transient, static, final in an entity are not persist-able, they are ignored to persist in data store. You can checkout the guide Jpa @Transient annotation for more details. This behaviour may work differently or may not work for different mapping APIs, so that it’s recommended to use @Transient annotation instead of java keywords to ignore fields to not persist in Data store.
  4. javax.persistence.Transient can be used for fields and methods.
  5. org.springframework.data.annotation.Transient can be used for fields, methods and annotations. You can apply transient capability to custom annotations using Spring’s Transient annotation.
  6. Apart from Spring @Transient and JPA @Transient annotations Neo4J also provides @Transient annotation. So, if you are working on Spring/Spring Boot Data with Neo4J then we have to use org.neo4j.ogm.annotation.Transient instead of Spring’s @Transient to ignore fields for persisting.

4. Conclusion

spring boot transientspring boot transient

  1. If you intended to ignore a field to not persist in data store, the recommended approach is using @Transient annotation instead of transient or static keywords because @Transient annotation (In Spring Data or JPA or Neo4J) is specific to data persistence.

you can check out source code at our git hub repository.

5. References

Ezoic

report this ad