Example of @Formula in Hibernate Annotation
February 01, 2013
Hibernate Annotation has some property annotation that provides different type of job. If we want to apply a virtual property in your entity to get a calculated value, we can use @Formula annotation to set the formula. This formula can be as complex as we want. In the example we are showing area of a Dimension entity.
Dimension.java
package com.concretepage.persistence; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.Formula; @Entity @Table(name="dimension") public class Dimension implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="id") private int id; @Column(name="length") private int length; @Column(name="width") private int width; @Formula(" length * width ") public long area; public Dimension(int id,int length, int width){ this.id=id; this.length=length; this.width=width; } public long getArea() { return area; } public void setArea(long area) { this.area = area; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } }
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping class="com.concretepage.persistence.Dimension"/> </session-factory> </hibernate-configuration>
package com.concretepage.util; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.concretepage.persistence.Dimension; public class HibernateUtil { private static final SessionFactory concreteSessionFactory; static { try { concreteSessionFactory = new AnnotationConfiguration() .configure().buildSessionFactory(); } 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(); session.beginTransaction(); Dimension d1 = new Dimension(1,2,5); session.save(d1); Dimension d2 = new Dimension(2,3,4); session.save(d2); session.getTransaction().commit(); session.refresh(d1); session.refresh(d2); Dimension d= (Dimension)session.get(Dimension.class,new Integer(1)); System.out.println(d.getArea()); } }
Hibernate: insert into dimension (length, width, id) values (?, ?, ?) Hibernate: insert into dimension (length, width, id) values (?, ?, ?) Hibernate: select dimension0_.id as id0_0_, dimension0_.length as length0_0_, dimension0_.width as width0_0_, dimension0_.length * dimension0_.width as formula0_0_ from dimension dimension0_ where dimension0_.id=? Hibernate: select dimension0_.id as id0_0_, dimension0_.length as length0_0_, dimension0_.width as width0_0_, dimension0_.length * dimension0_.width as formula0_0_ from dimension dimension0_ where dimension0_.id=? 10