Tech I Enjoy Logo

Custom Search




  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
Hibernate Transaction on JBoss :
Explaining Transaction using Hibernate
on JBoss Application Server.
Class Hierarchy Mapping Example :
class hierarchy mapping example using Hibernate
Framework and a simple to follow steps.
Hibernate Many to Many Mapping Example :
Many to many mapping example using Hibernate
Framework and a simple to follow steps.
Hibernate Interceptor Example :
Example on using Interceptor using Hibernate Framework
with source code explained.
Hibernate Example on composite Primary key :
Example on using Hibernate Framework
to work with mapping using composite
Primary key.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Hibernate class heirarchy mapping :
Hibernate Example on mapping
class hierarchy using various ways
of persisting into database
tables.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
List of Examples on Hibernate :
List of example using Hibernate.
Hibernate Property Formula :
Hibernate Example on Property
Tag with ease to do code walk-through
Hibernate Example on Filter :
Example on using Filter using Hibernate Framework
to work with.
Hibernate Bag Mapping Example :
class mapping using Bag Tag example using Hibernate
Framework and a simple to follow steps.
Hibernate one to one mapping Example :
one to one mapping explained using an example
and Hibernate Framework.
Hibernate Example on Filter Criteria :
Example on using Filter Criteria
using Hibernate Framework to work with.
Hibernate Component Property :
Hibernate Example on Component 
with source code explained.
Hibernate Join Example :
Using Table join explained with an example
while using Hibernate Framework.
Hibernate Insert Update control :
Hibernate Example on controlling
insert and update attributes
Hibernate Named Query Example :
Named Query markup using an example
and Hibernate Framework.
Hibernate Interview Questions :
Interview Questions on Hibernate with answer.
Example on persisting Class Hierarchy :
Example on using Hibernate Framework
to persist Class Hierarchy into database.


References :
Tags: Hibernate Class Hierarchy Persist example
Tags: Hibernate composite primary key
Tags: Hibernate criteria filter example
Tags: Hibernate Filter Example
Tags: Hibernate Interceptor example
Tags: Hibernate Interview Questions
Tags: Hibernate join example
Tags: Hibernate Many to Many Mapping Example
Tags: Hibernate mapping bag
Tags: Hibernate mapping class hierarchy table per subclass
Tags: Hibernate named query markup
Tags: Hibernate One to Many mapping example
Tags: Hibernate One To One Mapping Example
Tags: Hibernate scenario one to many
Tags: Hibernate transaction jboss
Tags: Hibernate



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions