Hibernate - @Transient Annotation with Example

Last Updated : 9 Apr, 2026

@Transient is used to exclude a field or property from being stored in the database. It is mainly used for calculated or temporary values that are only needed in application logic. Such fields are ignored during persistence and no column is created for them.

  • Used for non-persistent fields that are not part of database state
  • Helps keep database schema clean by avoiding unnecessary columns
  • Commonly used for derived or runtime-computed values

Example of @Transient Annotation 

Below examples demonstrate how @Transient is used to exclude fields or methods from persistence.

Example 1:

Java
@Entity
public class MathsOperation {

    // Primary Key
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long operationID;

    private String operation;
    private int num1;
    private int num2;

    // Default Constructor (Required by JPA)
    public MathsOperation() {
    }

    // Parameterized Constructor
    public MathsOperation(String operation, int num1, int num2) {
        this.operation = operation;
        this.num1 = num1;
        this.num2 = num2;
    }

    // Getters and Setters
    public Long getOperationID() {
        return operationID;
    }

    public void setOperationID(Long operationID) {
        this.operationID = operationID;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public int getNum1() {
        return num1;
    }

    public void setNum1(int num1) {
        this.num1 = num1;
    }

    public int getNum2() {
        return num2;
    }

    public void setNum2(int num2) {
        this.num2 = num2;
    }

    // Transient method (not stored in DB)
    @Transient
    public int getAnswer() {
        if ("add".equals(operation)) {
            return num1 + num2;
        } else if ("subtract".equals(operation)) {
            return num1 - num2;
        } else if ("multiply".equals(operation)) {
            return num1 * num2;
        } else if ("division".equals(operation)) {
            if (num2 == 0) {
                throw new ArithmeticException("Cannot divide by zero");
            }
            return num1 / num2;
        }
        return 0; // default case
    }
}

Explanation: This defines a MathsOperation entity where getAnswer() is marked as @Transient. The result is calculated at runtime and is not stored in the database.

Example 2:

Java
@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;
}

Explanation: This defines a ProductInfo entity where discountPrice is marked as @Transient, so it is not stored in the database. The value is derived from productPrice and used only at runtime, keeping computed data separate from persisted fields.

Comment