Hi jay vas,
we can mapping views in hibernate's hbm.xml file. As like model mapping we should be done followings:
* create view on the back end.(I used oracle view in my application)
* create a model for the view as like normal table model.
* Mapping the DBView & view model in .hbm.xml
[Here we should give view name instead of table name on table attribute,
id should be relation column name in view]
* Implement our model (with view) in the Application.
Example : View in DB : CREATE OR REPLACE VIEW MASTERVIEW (ITEM_CODE,ITEM_DES,UNIT_PRICE,TAX,VAT)
as Select a.item_code ITEM_CODE, a.item_des ITEM_DES, a.unit_price UNIT_PRICE , b.tax TAX ,b.vat VAT from item_master a inner join tax_master b on a.item_code = b.item_code
MasterView.java : import java.io.Serializable;
/*
* MasterView.java
*
* Created on January 28, 2008, 3:58 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author elangeswaran
*/
public class MasterView implements Serializable {
/** Creates a new instance of MasterView */
public MasterView() {
}
private String item_code;
private String item_des;
private double unti_price;
private double tax;
private double vat;
public String getItem_code() {
return item_code;
}
public void setItem_code(String item_code) {
this.item_code = item_code;
}
public String getItem_des() {
return item_des;
}
public void setItem_des(String item_des) {
this.item_des = item_des;
}
public double getUnti_price() {
return unti_price;
}
public void setUnti_price(double unti_price) {
this.unti_price = unti_price;
}
public double getTax() {
return tax;
}
public void setTax(double tax) {
this.tax = tax;
}
public double getVat() {
return vat;
}
public void setVat(double vat) {
this.vat = vat;
}
}
Hibenate Mapping file [MasterView.hbm.xml] <?xml version="1.0" encoding="UTF-8"?>
<!--
Document : MasterView.hbm.xml Created on : January 28, 2008, 10:29 AM
Author : elangeswaran
Description: Purpose of the document follows.
-->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-lazy="false">
<class name="com.myapp.dao.MasterView" table="">
<class name="MasterView" table="MASTERVIEW" mutable="false">
<id name="item_code" column="ITEN_CODE" type="string"></id>
<property name="item_des" column="ITEM_DES" type = "string"/>
<property name="unti_price" column="SUGGN_TITLE" type = "double" />
<property name="tax" column="TAX" type = "double" />
<property name="vat" column="VAT" type = "double" />
</class>
</hibernate-mapping>
I thing above explaination would be help to use Db View with Hibenate Mapping in j2ee application. If you want more information reply to me..
Regards,
K.Elanges