Home >> Hibernate
Is it possible to have some fields in business POJO class whose value
is not directly retrieved from a single column from the table that is
used for mapping this class files fields?
Yes, there is an attribute as formula in the property tag under a class
tag, which can be used to assign derived values from one or many columns
and using some expression also.
Let us explore this with an example:
|
|  |
|
I have created an environment for creating a Java project using ECLIPSE
as IDE and copied required JAR files from the Hibernate Framework
distribution to this Java project lib folder so as to be able to
create this example, compile and test this example from within the
Eclipse environment.
The POJO bean class is having hypothetical data of an employee with
salary of X amount, but trick is to define a field in this class
as basicComponent which doesn't have to have a column in the database
table, still when an object of this class is retrieved from database,
this field 'basicComponent' should automatically be filled with
appropriate value based on a formula attribute that is defined
in the property tag for this field 'basicComponent'.
SampleBean.java
package com.techienjoy.hibernate.example;
import java.math.BigDecimal;
public class SampleBean {
long id;
String empId;
String name;
int age;
String addrLine1;
String addrLine2;
String phone;
BigDecimal salary;
BigDecimal basicComponent;
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public BigDecimal getBasicComponent() {
return basicComponent;
}
public void setBasicComponent(BigDecimal basicComponent) {
this.basicComponent = basicComponent;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddrLine1() {
return addrLine1;
}
public void setAddrLine1(String addrLine1) {
this.addrLine1 = addrLine1;
}
public String getAddrLine2() {
return addrLine2;
}
public void setAddrLine2(String addrLine2) {
this.addrLine2 = addrLine2;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
|
idea here is to show this basicComponent on console output as 1440.
But there should not be a column required for this field to map to.
Example.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.techienjoy.hibernate.example">
<class name="SampleBean" table="SAMPLE_BEAN">
<id name="id" type="long" column="ID"/>
<property name="empId" type="string" column="EMPID" />
<property name="name" type="string" column="NAME"/>
<property name="age" type="int" column="AGE"/>
<property name="addrLine1" type="string" column="ADDRLINE1"/>
<property name="addrLine2" type="string" column="ADDRLINE2"/>
<property name="phone" type="string" column="PHONE"/>
<property name="salary" type="big_decimal" column="SALARY"/>
<property name="basicComponent" type="big_decimal" formula="salary * 12/100"/>
</class>
</hibernate-mapping>
|
Testing this example can be done using following Test Harness Code:
Test.java
package com.techienjoy.hibernate.example;
/**
* This code is provided on "AS IS" basis, without warranty
* or guaranty of any kind.
* Author : TechIEnjoy
* Date: 28-November-2010
*/
import java.math.BigDecimal;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
private static SessionFactory sessionFactory;
/**
* @param args
*/
public static void main(String[] args) {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
SampleBean a = new SampleBean();
a.setId(1001);
a.setEmpId("E001");
a.setName("Johney Rieder");
a.setAddrLine1("Address Line 2");
a.setAddrLine2("Address Line 2");
a.setAge(45);
a.setPhone("333-333-3333");
a.setSalary(new BigDecimal(12000));
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.begin();
session.saveOrUpdate(a);
session.flush();
tx.commit();
session.close();
Session session1 = sessionFactory.openSession();
Transaction tx1 = session1.beginTransaction();
tx1.begin();
SampleBean b = (SampleBean) session1.load(SampleBean.class, new Long(1001));
System.out.println("Basic Component from Salary: "+b.getBasicComponent());
tx1.commit();
session1.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
On execution of this test code, we can have a table automatically created and
populated with values as defined in the Test code.

Hope this helps.
If anything missed out , please let me know at
techienjoy at yahoo . com