In hibernate, we have many annotations. Each annotation has its importance to perform an operation. Likewise, @Formula is a hibernate annotation to calculate the dynamic value and assign that value to the property.

@Formula annotation takes the expression as a parameter, and at fetch time it will evaluate the expression and assigns an evaluated value to the property.

This @Formula parameter can be as complex as we want. That means, it may be a simple expression or it may be a complex query, this can be applied on top of the property.

In this tutorial, we are going to calculate the Employee total salary, using the @Formula annotation in hibernate.

@Formula Hibernate Example :

To make the example as simple, we figure the total employee salary like below.

Employee total salary = basic + conveyance + hra;

Let’s implement this calculation by using @Formula annotation in hibernate. Project Structure:

@formula Example

Required Dependencies :

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

    <!-- Generic properties -->

    <!-- Spring -->

    <!-- Hibernate / JPA -->

    <!-- Logging -->

    <!-- Test -->

    <!-- Hibernate -->
    <!-- MySQL Driver -->

Hibernate POJO :
package com.onlinetutorialspoint.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Formula;

@Table(name = "salaries")
public class Salaries {
    @Column(name = "empid")
    private int empId;
    @Column(name = "empname")
    private String empName;
    @Column(name = "basic")
    private int basic;
    @Column(name = "conveyance")
    private int conveyance;
    @Column(name = "hra")
    private int hra;

    @Formula(" basic + conveyance + hra ")
    private float total;

    public int getEmpId() {
        return empId;

    public void setEmpId(int empId) {
        this.empId = empId;

    public String getEmpName() {
        return empName;

    public void setEmpName(String empName) {
        this.empName = empName;

    public int getBasic() {
        return basic;

    public void setBasic(int basic) {
        this.basic = basic;

    public int getConveyance() {
        return conveyance;

    public void setConveyance(int conveyance) {
        this.conveyance = conveyance;

    public int getHra() {
        return hra;

    public void setHra(int hra) {
        this.hra = hra;

    public float getTotal() {
        return total;

    public void setTotal(float total) { = total;


Let’s Run the Example :
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import com.onlinetutorialspoint.bean.Salaries;

public class Main {
    private static final SessionFactory concreteSessionFactory;
    static {
        try {
            concreteSessionFactory = new AnnotationConfiguration()
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
    public static Session getSession()
            throws HibernateException {
        return concreteSessionFactory.openSession();

    public static void main(String[] args) {
        Session session=getSession();
        Transaction tx = session.beginTransaction();
        Salaries salaries = new Salaries();
        Salaries d=  (Salaries)session.get(Salaries.class,new Integer(1));    
        System.out.println("Employee Total Salary :"+d.getTotal());



On the above example, we use session.get(Salaries.class, new Integer(1)) to get the Salaries details from hibernate cache or database.

Hibernate: insert into salaries (basic, conveyance, empname, hra, empid) values (?, ?, ?, ?, ?)
Hibernate: select salaries0_.empid as empid1_0_0_, salaries0_.basic as basic2_0_0_, salaries0_.conveyance as conveyan3_0_0_, salaries0_.empname as empname4_0_0_, salaries0_.hra as hra5_0_0_,  salaries0_.basic + salaries0_.conveyance + salaries0_.hra  as formula0_0_ from salaries salaries0_ where salaries0_.empid=?
Employee Total Salary :20000.0

On the above example, we use @Formula annotation for simple expression. If we want to make this some more extent, we do.. Like below.

@Formula("(select min(s.hra) from salaries s) ")
private float total;

If you change the code like above, you can get the minimum hra in salaries table.

Happy Learning 🙂

Download Example

About the Author:

Founder of Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

One Comment

  1. Avatar
    Sumit July 17, 2020 at 1:45 pm - Reply

    I am seeing the below error. My POJO looks like this:

    private Long longOldValue;
    private String oldValue;
    private Long longNewValue;
    private String newValue;
    private long created;
    private String reasonChange;
    private Long difference;

    java.sql.SQLSyntaxErrorException: Unknown column ‘applicatio1_.longNewValue’ in ‘field list’
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException( ~[mysql-connector-java-8.0.19.jar:8.0.19]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException( ~[mysql-connector-java-8.0.19.jar:8.0.19]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException( ~[mysql-connector-java-8.0.19.jar:8.0.19]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal( ~[mysql-connector-java-8.0.19.jar:8.0.19]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery( ~[mysql-connector-java-8.0.19.jar:8.0.19]

Leave A Comment