This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes hibernate views Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate views" Watch "hibernate views" New topic
Author

hibernate views

jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
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

Joined: Dec 13, 2007
Posts: 17
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


Regards,<br />K.Elanges
Elanges waran
Greenhorn

Joined: Dec 13, 2007
Posts: 17
Hi Jay,

Kindly note this : table="MASTERVIEW" in MaterView.hbm.xml. I missed this attribute...
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Thanks --- what is "MASTERVIEW" is that a hibernate view
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Nilesh Pat
Ranch Hand

Joined: Mar 01, 2010
Posts: 46
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


Regards,
Nil
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hibernate views
 
Similar Threads
Good books or tutorial site for JDO
[SCBCD 5.0] Simplified (reduced) SELECT clause form
WA #1.....word association
Where to put questions regarding Hybernet?
how to cast String to Clob ?