Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate views

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys : Is it possible map a view in hibernate ? I can't find any documentation on it but it seems like a logical thing that one should be able to do for often executed queries.
 
Elanges waran
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Elanges waran
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jay,

Kindly note this : table="MASTERVIEW" in MaterView.hbm.xml. I missed this attribute...
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks --- what is "MASTERVIEW" is that a hibernate view
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jay vas:
Thanks --- what is "MASTERVIEW" is that a hibernate view


I believe that should be the name of the view in the database, Hibernate doesn't have a View object.

Mark
 
Nilesh Pat
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have view created on two keys(composite kyes) like




How to use it as <id> in hbm.xml?
What is the primary key here?
i.e.
<id name="item_code" column="ITEM_CODE" type="string"></id>

i can't able to use criteria on such view. Getting duplicate values in criteria.list()

Please help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic