在Hibernate应用中使用视图

Hibernate对于一些函数(如extract 、union,oracle数据库)不支持,导致用hql语句实现一些功能很麻烦,所以用了视图,
[color=red]下面是在oracle数据库上的使用,工具MyEclipse。[/color][color=red]
1. 建立视图[/color]
例如:更加需要,建立视图cash_flow
[code]
create or replace view cash_flow as
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[/code]

[color=red]2、导出类和配置文件[/color]
在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继承这个父类。
下面为到出的代码
[code]public class CashFlowId implements java.io.Serializable {

// Fields
private Date gatherDate;
private String description;
private Long gahterSum;
private String groupCn;

// Constructors

/** default constructor */
public CashFlowId() {
}

/** full constructor */
public CashFlowId(Date gatherDate, String description, Long gahterSum, String groupCn) {
this.gatherDate = gatherDate;
this.description = description;
this.gahterSum = gahterSum;
this.groupCn = groupCn;
}


// Property accessors

getter、setter方法
.............

public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof CashFlowId) ) return false;
CashFlowId castOther = ( CashFlowId ) other;

return ( (this.getGatherDate()==castOther.getGatherDate()) || ( this.getGatherDate()!=null && castOther.getGatherDate()!=null && this.getGatherDate().equals(castOther.getGatherDate()) ) )
&& ( (this.getDescription()==castOther.getDescription()) || ( this.getDescription()!=null && castOther.getDescription()!=null && this.getDescription().equals(castOther.getDescription()) ) )
&& ( (this.getGahterSum()==castOther.getGahterSum()) || ( this.getGahterSum()!=null && castOther.getGahterSum()!=null && this.getGahterSum().equals(castOther.getGahterSum()) ) )
&& ( (this.getGroupCn()==castOther.getGroupCn()) || ( this.getGroupCn()!=null && castOther.getGroupCn()!=null && this.getGroupCn().equals(castOther.getGroupCn()) ) );
}

public int hashCode() {
int result = 17;

result = 37 * result + ( getGatherDate() == null ? 0 : this.getGatherDate().hashCode() );
result = 37 * result + ( getDescription() == null ? 0 : this.getDescription().hashCode() );
result = 37 * result + ( getGahterSum() == null ? 0 : this.getGahterSum().hashCode() );
result = 37 * result + ( getGroupCn() == null ? 0 : this.getGroupCn().hashCode() );
return result;
}
}[/code]

[code]public abstract class AbstractCashFlow implements java.io.Serializable {

private CashFlowId id;

// Constructors
/** default constructor */
public AbstractCashFlow() {
}

/** full constructor */
public AbstractCashFlow(CashFlowId id) {
this.id = id;
}

// Property accessors

public CashFlowId getId() {
return this.id;
}

public void setId(CashFlowId id) {
this.id = id;
}
}[/code]
[code]public class CashFlow extends AbstractCashFlow implements java.io.Serializable {
// Constructors
/** default constructor */
public CashFlow() {
}
/** full constructor */
public CashFlow(CashFlowId id) {
super(id);
}
}[/code]
[code]<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.taiji.costmatrix.model.CashFlow" table="CASH_FLOW">
<composite-id name="id" class="com.taiji.costmatrix.model.CashFlowId">
<key-property name="gatherDate" type="java.util.Date">
<column name="GATHER_DATE" length="7" />
</key-property>
<key-property name="description" type="java.lang.String">
<column name="DESCRIPTION" length="1000" />
</key-property>
<key-property name="gahterSum" type="java.lang.Long">
<column name="GAHTER_SUM" precision="22" scale="0" />
</key-property>
<key-property name="groupCn" type="java.lang.String">
<column name="GROUP_CN" length="256" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>[/code]

[color=red]3.调用[/color]
别忘了将hbm文件的路径加到hibernate mapping那
这是就可以直接在dao里hql语句读取,就像操作其他表一样.
但须注意写法.
[code]from CashFlow cf where cf.id.groupCn=”T”[/code]
也可以直接写sql
如[code]Select * from cash_flow[/code]。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值