Hibernate对于一些函数(如extract 、union,oracle数据库)不支持,导致用hql语句实现一些功能很麻烦,所以用了视图,
下面是在oracle数据库上的使用,工具MyEclipse。
1. 建立视图
例如:更加需要,建立视图cash_flow
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
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继承这个父类。
下面为到出的代码
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;
}
}
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;
}
}
public class CashFlow extends AbstractCashFlow implements java.io.Serializable {
// Constructors
/** default constructor */
public CashFlow() {
}
/** full constructor */
public CashFlow(CashFlowId id) {
super(id);
}
}
<?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>
3.调用
别忘了将hbm文件的路径加到hibernate mapping那
这是就可以直接在dao里hql语句读取,就像操作其他表一样.
但须注意写法.
from CashFlow cf where cf.id.groupCn=”T”
也可以直接写sql
如
Select * from cash_flow
。