The Wayback Machine - https://web.archive.org/web/20240728070905/https://www.geeksforgeeks.org/hibernate-transient-annotation-with-example/
Open In App

Hibernate – @Transient Annotation with Example

Last Updated : 07 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

@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.



Similar Reads

Hibernate - @Version Annotation with Example
@Version annotation is used to specify the version number for a specific entity. Version Number provided using @Version annotation is used to prevent concurrent modification to an entity. When an entity is being updated, the version number is also incremented. If another transaction tries to update the same entity with the older version number, an
3 min read
Hibernate - @OrderBy Annotation with Example
@OrderBy annotation is used in Hibernate to specify the ordering of the elements in the collection valued property of an entity class. It is used to define the order of the elements which should be displayed. We can order the data within the entity using a specific parameter in ascending or descending order. Examples of @OrderBy AnnotationExample 1
4 min read
Difference between volatile and transient keywords in Java
Just like any other programming language, Java has a set of keywords which are reserved and have a special meaning. In this article, we will see the difference between the keywords volatile and transient. Before getting into the differences, let us first understand what each of them actually means. Volatile: The Volatile keyword is used to mark the
3 min read
transient keyword in Java
transient is a variables modifier used in serialization. At the time of serialization, if we don't want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type. transient keyword plays an impo
3 min read
Spring Boot - Difference Between @Service Annotation and @Repository Annotation
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. @Service Annotation In an application, the business logic resides
7 min read
Hibernate - Table Per Subclass using Annotation
Hibernate is an open-source, non-invasive, lightweight java ORM(Object-relational mapping) framework that is used to develop persistence logic that is independent of Database software. An ORM(Object-relational mapping) framework simplifies data creation, data manipulation, and data access. It is a programming technique that maps the object to the d
6 min read
Hibernate - Table Per Hierarchy using Annotation
Hibernate is a framework that provides some abstraction layer, meaning that the programmer does not have to worry about the implementations, it does the implementations for you internally like writing queries to perform CRUD operations, establishing a connection with the database, etc. It is an open-source, non-invasive, lightweight java ORM(Object
8 min read
Hibernate - Table Per Concrete Class Using Annotation
Hibernate is a framework that provides some abstraction layer, meaning that the programmer does not have to worry about the implementations, it does the implementations for you internally like writing queries to perform CRUD operations, establishing a connection with the database, etc. It is an open-source, non-invasive, lightweight java ORM(Object
7 min read
Hibernate - @Embeddable and @Embedded Annotation
The @Embeddable and @Embedded annotations in Hibernate are used to map an object’s properties to columns in a database table. These annotations are used in combination to allow the properties of one class to be included as a value type in another class and then be persisted in the database as part of the containing class. OverviewThe @Embeddable an
4 min read
Hibernate - @GeneratedValue Annotation in JPA
@GeneratedValue annotation, the name itself suggests that it will generate something. This annotation is generally used in conjunction with @Id annotation to automatically generate unique values for primary key columns within our database tables. When creating an entity class we have to specify a primary key for that entity. For marking the field p
3 min read
Practice Tags :
three90RightbarBannerImg