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
。
分享到:
相关推荐
本文分析了Hibernate和Struts的机制,提出了一种基于Hibernate和Struts的J2EE应用开发...在这种策略中,模型层用Hibernate实现,视图和控制器则用Struts框架实现。这样可大大降低代码的耦合性以及提高系统的开发效率。
使用struts+spring+hibernate 组装web应用 大部分的Web应用在职责上至少能被分成4层。 这四层是:presentation(描述也就是视图层),persistence(持久也就是数据持久层),business(业务)和domain model(域...
6.3.6 在代码中使用Log4j 6.4 改进Log4j 6.5 小结 第七章 Ant使用指南 7.1 Ant介绍 7.1.1 Ant简介 7.1.2 为什么要使用Ant 7.2 建立Ant的开发环境 7.2.1 下载Ant 7.2.2 配置Ant 7.3 Ant的使用方法 7.3.1 Ant能完成的...
本文分析了Hibernate和Struts的机制,提出了一种基于Hibernate和Struts的J2EE应用开发...在这种策略中,模型层用Hibernate实现,视图和控制器则用Struts框架实现。这样可大大降低代码的耦合性以及提高系统的开发效率。
本文分析了Hibernate和Struts的机制,提出了一种基于Hibernate和Struts的J2EE应用开发...在这种策略中,模型层用Hibernate实现,视图和控制器则用Struts框架实现。这样可大大降低代码的耦合性以及提高系统的开发效率。
该案例采用目前最流行、最规范的Java EE架构,整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。 本书不再介绍Struts 1.x相关内容,如果读者希望...
整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。该案例既提供了IDE无关的、基于Ant管理的项目源码,也提供了基于Eclipse IDE的项目源码,最大...
本文分析了Hibernate和Struts的机制,提出了一种基于Hibernate和Struts的J2EE应用开发...在这种策略中,模型层用Hibernate实现,视图和控制器则用Struts框架实现。这样可大大降低代码的耦合性以及提高系统的开发效率。
6.3.6 在代码中使用Log4j 6.4 改进Log4j 6.5 小结 第七章 Ant使用指南 7.1 Ant介绍 7.1.1 Ant简介 7.1.2 为什么要使用Ant 7.2 建立Ant的开发环境 7.2.1 下载Ant 7.2.2 配置Ant 7.3 Ant的使用方法 7.3.1 Ant能完成的...
整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。该案例既提供了IDE无关的、基于Ant管理的项目源码,也提供了基于Eclipse IDE的项目源码,最大...
6.3.6 在代码中使用Log4j 6.4 改进Log4j 6.5 小结 第七章 Ant使用指南 7.1 Ant介绍 7.1.1 Ant简介 7.1.2 为什么要使用Ant 7.2 建立Ant的开发环境 7.2.1 下载Ant 7.2.2 配置Ant 7.3 Ant的使用方法 7.3.1 Ant能完成的...
在 NetBeans 8.0.x 中详细阐述的项目,它包含一个 Web 应用程序的基本示例,该应用程序使用 MVC 模式通过 servlet 和 JSP 实现表(书籍)的 CRUD 功能。 Hibernate 4.3 用于访问数据库。 对于视图的设计,包含了 ...
这个资料包包含了一个完整的示例项目,用于演示如何使用Struts和Hibernate框架来构建一个可扩展、易于维护的Web应用程序。在这个示例项目中,Struts框架负责处理用户的请求和响应,以及与视图层的交互。它提供了一个...
整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。该案例既提供了IDE无关的、基于Ant管理的项目源码,也提供了基于Eclipse IDE的项目源码,最大...
整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。该案例既提供了IDE无关的、基于Ant管理的项目源码,也提供了基于Eclipse IDE的项目源码,最大...
整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。该案例既提供了IDE无关的、基于Ant管理的项目源码,也提供了基于Eclipse IDE的项目源码,最大...
整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。该案例既提供了IDE无关的、基于Ant管理的项目源码,也提供了基于Eclipse IDE的项目源码,最大...
整个应用分为领域对象层、DAO层、业务逻辑层、MVC层和视图层,各层之间分层清晰,层与层之间以松耦合的方法组织在一起。该案例既提供了IDE无关的、基于Ant管理的项目源码,也提供了基于Eclipse IDE的项目源码,最大...
通过这个资料包,用户可以深入了解和学习如何在Web应用程序中实现一个图书管理系统,掌握如何将Struts、Hibernate和Spring框架整合在一起,实现分层架构、MVC设计模式以及ORM映射等关键技术。Struts作为一款优秀的...
12.2 在Spring中使用Hibernate 12.2.1 配置SessionFactory 12.2.2 使用HibernateTemplate 12.2.3 处理LOB类型数据 12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务...