@Transient annotation in Hibernate is used to mark a property or field in an entity class as transient. This means that the field or property marked as transient should be excluded when the data persists in the database. Wherever an entity is mapped to a database table, by default all the non-transient fields and properties are persisted. There are certain fields or properties in an entity that should be ignored during the data persistence process such as the data which is being calculated for displaying, temporary variables, and other data which is relevant only within the application logic but is not required in the database. By using the @Transient annotation to that field or a property Hibernate can exclude that field from the persistence process and the corresponding column will not be created in the database table.
Example of @Transient Annotation
Example 1:
Java
// on the below line we are creating an entity for // MathsOperation class. @Entity public class MathsOperation { // on the below line we are creating a variable for // operation id. @Id @GeneratedValue private Long operationID; // on the below line we are creating a string variable // for operation which we have to perform . private String operation; // on the below line we are creating two variables for // num1 and num2 private int num1; private int num2; // on the below line we are creating a method for // getAnswer which will return the answer based on the // operation specified by the user. for the below method // we are specifying annotation as Transient which will // exclude that method from data persistence in the // database. @Transient private int getAnswer() { if (operation == "add" ) { return num1 + num2; } else if (operation == "subtract" ) { return num1 - num2; } else if (operation == "multiply" ) { return num1 * num2; } else if (operation == "division" ) { return num1 / num2; } } } |
Example Explanation: In the above example, the MathsOperation entity class has a field for operationID, operation name, num1, and num2. After that, we created a method as getAnswer and marked it as @Transient to specify that it should not be considered for persistence. The method calculates the answer for num1 and num2 based on the operation which is specified, but the answer which will be generated will not be stored in the database. By using @Transient we can exclude the specific field or method from being persisted in our database. In this case, we are adding a @Transient annotation for our getAnswer method to indicate that the method is not a part of the persistent state of the MathsOperation entity.
Example 2:
Java
// on the below line we are creating an entity for the // ProductInfo class. @Entity public class ProductInfo { // on the below line we are creating a variable for // product id. @Id @GeneratedValue private Long productID; // on the below line we are creating a string variable // for product name private String productName; // on the below line creating a variable for product // price. private int productPrice; // on the below line we are creating a variable for // discounted price and marking it as transient to // exclude it from data persistence in the database. @Transient private int discountPrice = productPrice * 5 / 100 ; } |
Example Explanation: In the above example, we are creating a Product Info entity class which has a field for productID, product name, product price a,d a discounted price. We are marking the discounted price property as @Transient because we have to exclude the value of this variable from persisting in the database. We are calculating the value for a discounted price from the product price by using a specific multiplication factor. Marking it with @Transient annotation will exclude this variable from adding it to our database operations.