在Hibernate应用中使用视图

本文介绍如何在Oracle数据库中创建视图,并通过MyEclipse工具将其与Hibernate框架集成,实现复杂的查询功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hibernate对于一些函数(如extract 、union,oracle数据库)不支持,导致用hql语句实现一些功能很麻烦,所以用了视图,
下面是在oracle数据库上的使用,工具MyEclipse。
1. 建立视图

例如:更加需要,建立视图cash_flow

代码
  1. create or replace view cash_flow as   
  2. select gather_date,description,gahter_sum,group_cn from actual_gather ag where(ag.gahter_sum>0) union all select expense_time,remark,(0 - expense_sum),group_cn from cash_expense ce where (ce.expense_sum >0) order by gather_date desc  

 

2、导出类和配置文件
在MyEclipse Database explorer视图下,点击右键可以建立数据库连接文件,成功连接上数据库后,在TABLE下面是常用的导出表所对应配置文件和java类功能。
即一个java基类AbstractModel.java,一个子类Model.java,和一个配置文件Model.hbm
而在VIEW下面,MyEclipse将把其中的视图当作表来导出。但导出的文件略有不同,多一个java类。
即AbstractCashFlow.java,CashFlow.java,CashFlowId.java,CashFlow.hbm等四个文件
其中CashFlowId类的属性就是视图的的属性,并以这些属性作为联合主键。
作为AbstractCashFlow的主键和唯一的属性. CashFlow继承这个父类。
下面为到出的代码

代码
  1. public class CashFlowId  implements java.io.Serializable {   
  2.   
  3.     // Fields       
  4.      private Date gatherDate;   
  5.      private String description;   
  6.      private Long gahterSum;   
  7.      private String groupCn;   
  8.   
  9.     // Constructors   
  10.   
  11.     /** default constructor */  
  12.     public CashFlowId() {   
  13.     }   
  14.        
  15.     /** full constructor */  
  16.     public CashFlowId(Date gatherDate, String description, Long gahterSum, String groupCn) {   
  17.         this.gatherDate = gatherDate;   
  18.         this.description = description;   
  19.         this.gahterSum = gahterSum;   
  20.         this.groupCn = groupCn;   
  21.     }   
  22.        
  23.       
  24.     // Property accessors   
  25.   
  26.     getter、setter方法   
  27.     .............   
  28.   
  29.    public boolean equals(Object other) {   
  30.          if ( (this == other ) ) return true;   
  31.          if ( (other == null ) ) return false;   
  32.          if ( !(other instanceof CashFlowId) ) return false;   
  33.          CashFlowId castOther = ( CashFlowId ) other;    
  34.             
  35.          return ( (this.getGatherDate()==castOther.getGatherDate()) || ( this.getGatherDate()!=null && castOther.getGatherDate()!=null && this.getGatherDate().equals(castOther.getGatherDate()) ) )   
  36.  && ( (this.getDescription()==castOther.getDescription()) || ( this.getDescription()!=null && castOther.getDescription()!=null && this.getDescription().equals(castOther.getDescription()) ) )   
  37.  && ( (this.getGahterSum()==castOther.getGahterSum()) || ( this.getGahterSum()!=null && castOther.getGahterSum()!=null && this.getGahterSum().equals(castOther.getGahterSum()) ) )   
  38.  && ( (this.getGroupCn()==castOther.getGroupCn()) || ( this.getGroupCn()!=null && castOther.getGroupCn()!=null && this.getGroupCn().equals(castOther.getGroupCn()) ) );   
  39.    }   
  40.       
  41.    public int hashCode() {   
  42.          int result = 17;   
  43.             
  44.          result = 37 * result + ( getGatherDate() == null ? 0 : this.getGatherDate().hashCode() );   
  45.          result = 37 * result + ( getDescription() == null ? 0 : this.getDescription().hashCode() );   
  46.          result = 37 * result + ( getGahterSum() == null ? 0 : this.getGahterSum().hashCode() );   
  47.          result = 37 * result + ( getGroupCn() == null ? 0 : this.getGroupCn().hashCode() );   
  48.          return result;   
  49.    }      
  50. }  

 

 

代码
  1. public abstract class AbstractCashFlow  implements java.io.Serializable {   
  2.   
  3.      private CashFlowId id;   
  4.   
  5.     // Constructors   
  6.     /** default constructor */  
  7.     public AbstractCashFlow() {   
  8.     }   
  9.   
  10.     /** full constructor */  
  11.     public AbstractCashFlow(CashFlowId id) {   
  12.         this.id = id;   
  13.     }   
  14.       
  15.     // Property accessors   
  16.   
  17.     public CashFlowId getId() {   
  18.         return this.id;   
  19.     }   
  20.        
  21.     public void setId(CashFlowId id) {   
  22.         this.id = id;   
  23.     }   
  24. }  

代码
  1. public class CashFlow extends AbstractCashFlow implements java.io.Serializable {   
  2.     // Constructors   
  3.     /** default constructor */  
  4.     public CashFlow() {   
  5.     }   
  6.     /** full constructor */  
  7.     public CashFlow(CashFlowId id) {   
  8.         super(id);           
  9.     }       
  10. }  

代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--   
  5.     Mapping file autogenerated by MyEclipse - Hibernate Tools  
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="com.taiji.costmatrix.model.CashFlow" table="CASH_FLOW">  
  9.         <composite-id name="id" class="com.taiji.costmatrix.model.CashFlowId">  
  10.             <key-property name="gatherDate" type="java.util.Date">  
  11.                 <column name="GATHER_DATE" length="7" />  
  12.             </key-property>  
  13.             <key-property name="description" type="java.lang.String">  
  14.                 <column name="DESCRIPTION" length="1000" />  
  15.             </key-property>  
  16.             <key-property name="gahterSum" type="java.lang.Long">  
  17.                 <column name="GAHTER_SUM" precision="22" scale="0" />  
  18.             </key-property>  
  19.             <key-property name="groupCn" type="java.lang.String">  
  20.                 <column name="GROUP_CN" length="256" />  
  21.             </key-property>  
  22.         </composite-id>  
  23.     </class>  
  24. </hibernate-mapping>  

 

3.调用
别忘了将hbm文件的路径加到hibernate mapping那
这是就可以直接在dao里hql语句读取,就像操作其他表一样.
但须注意写法.

代码
  1. from CashFlow cf  where cf.id.groupCn=”T”  

也可以直接写sql
代码
  1. Select * from cash_flow  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值