• transient is to exclude all serializations (over the wire, saving to disk, saving to DB)
  • @javax.persistence.Transient is specifically for JPA DB serialization
  • @org.springframework.data.annotation.Transient is for ObjectMapping Framework serializations used within Spring

Code Examples

@Entity
public class User {

	@Id
	private long id;

	@Column(name = "username")
	private String username;

	@Column(name = "email")
	private String email;

	@Column(name = "password")
	private String password;
}

Now to map this java entity object to JSON format you can either use a mapping framework (e.g jackson: com.fasterxml.jackson.databind.ObjectMapper) or do it manually.

The JSON format output that you would get when to convert user 2 object to JSON is:

{
   "id": 2,
   "email": "test03@gmail.com",
   "username": "test03",
   "password": "$2a$10$UbvmdhfcKxSNr/I4CjOLtOkKGX/j4/xQfFrv3FizxwEVk6D9sAoO"
}

Now if you added:

@org.springframework.data.annotation.Transient
@Column(name = "password")
private String password;

and then used the Mapping Framework to again generate the JSON for the user 2 entity you would get:

{
   "id": 2,
   "email": "test03@gmail.com",
   "username": "test03",
}

Note the password field is missing from you JSON output. Thats because @org.springframework.data.annotation.Transient specifically states to the spring framework that the Object Mapper you are using should not include this value when converting from Java Object to JSON