This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate views" Watch "hibernate views" New topic

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

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 :

as Select a.item_code ITEM_CODE, a.item_des ITEM_DES, a.unit_price UNIT_PRICE , TAX ,b.vat VAT from item_master a inner join tax_master b on a.item_code = b.item_code :

* 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) { = 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"


<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" />

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,<br />K.Elanges
Elanges waran

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

Joined: Feb 05, 2001
Posts: 17276

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.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Nilesh Pat
Ranch Hand

Joined: Mar 01, 2010
Posts: 46

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?
<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

I agree. Here's the link:
subject: hibernate views
It's not a secret anymore!