Friday, 20 December 2013

Derived Property , computed property @Formula annotation in Hibernate


A derived, or calculated property when we want the Database to do some computation for you rather than in the JVM, we can create some kind of virtual column. We can use a SQL formula instead of mapping a property into a column.This kind of property is read only calculated at run time ,or fetch time.



To configure the derived property we use the @Formula annotation or formula attribute in xml.

// perform calculations
    @Formula(value="msgQuantity*2")
 private int maxId;
 
     // call functions
     @Formula(value = "MOD(msgQuantity,2)")
 private int sqrt;


Sample Code :-

ChatMassage.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javastoreroom.modal;

import java.io.Serializable;

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

import org.hibernate.annotations.Formula;

/**
 * 
 * @author Arun
 */
@Entity
@Table(name = "messages")
public class ChatMassage implements Serializable {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "msg_id")
 private int msg_id;

 @Column(name = "message")
 private String message;

 @Column(name = "msgQuantity")
 private int quantity;

 @Formula(value = "MOD(msgQuantity,2)")
 private int sqrt;

 @Formula(value="msgQuantity*2")
 private int maxId;
 
 public int getMsg_id() {
  return msg_id;
 }

 public void setMsg_id(int msg_id) {
  this.msg_id = msg_id;
 }

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }

 public int getQuantity() {
  return quantity;
 }

 public void setQuantity(int quantity) {
  this.quantity = quantity;
 }

 public int getSqrt() {
  return sqrt;
 }

 public void setSqrt(int sqrt) {
  this.sqrt = sqrt;
 }

 public int getMaxId() {
  return maxId;
 }

 public void setMaxId(int maxId) {
  this.maxId = maxId;
 }
 
}


Main.java
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.javastoreroom.hibernateutil.HibernateUtil;
import com.javastoreroom.modal.ChatMassage;

public class Main {

 public static void main(String[] args) {
  Session session = HibernateUtil.getSessionFactory().openSession();

  try {
   
       Criteria criteria = session.createCriteria(ChatMassage.class);
   if (criteria.list().size() > 0)
    viewMassage(criteria);

  } catch (HibernateException e) {
   e.printStackTrace();
  } finally {
   session.close();
  }
 }

 @SuppressWarnings("unchecked")
 private static void viewMassage(Criteria criteria) {
  List chatMassages = criteria.list();
 
  for (ChatMassage chatMassage : chatMassages){ 
   System.out.println(chatMassage.getMessage());
           System.out.println(chatMassage.getSqrt());
   System.out.println(chatMassage.getMaxId());
  }
 }

}

While execute code see below query :-

select
        this_.msg_id as msg1_0_0_,
        this_.message as message0_0_,
        this_.msgQuantity as msgQuant3_0_0_,
        this_.msgQuantity*2 as formula0_0_,
        MOD(this_.msgQuantity,
        2) as formula1_0_ 
    from
        messages this_

That's done :)

No comments:

Post a Comment