本人最近在技术转型过程中,从DELPHI开发平台转到JAVA开发平台。为了到达好的学习效果,我准备改造以前自己用DELPHI做的报表系统。在改造过程中,我决定用Hibernate做为持久化中间件。经过两个星期的学习和开发过程中,发现Hibernate并不适合做报表(也许我对HB的理解还不够吧
,希望牛人出来指点一下 )。如下面实例:
表结构:
create table dbo.owner_serverfee (
owner_no char(4) not null,
ls_name varchar(255) not null,
area_name varchar(32) null,
incomefee decimal(14, 2) not null,
truemonth char(6) not null,
addfee decimal(14, 2) null,
reducefee decimal(14, 2) null,
gen_time datetime not null,
constraint PK_no_month PRIMARY KEY CLUSTERED (owner_no,ls_name,truemonth )
)
通过MyEclipse生成对应的映射文件和POJO:
<?xml version="1.0" encoding="utf-8"?>
<!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.acidpie.bean.OwnerServerfee" table="owner_serverfee" schema="dbo" catalog="center06">
<composite-id name="id" class="com.acidpie.bean.OwnerServerfeeId">
<key-property name="lsName" type="java.lang.String">
<column name="ls_name" />
</key-property>
<key-property name="ownerNo" type="java.lang.String">
<column name="owner_no" length="4" />
</key-property>
<key-property name="truemonth" type="java.lang.String">
<column name="truemonth" length="6" />
</key-property>
</composite-id>
<property name="areaName" type="java.lang.String">
<column name="area_name" length="32" />
</property>
<property name="incomefee" type="java.lang.Double">
<column name="incomefee" precision="14" not-null="true" />
</property>
<property name="addfee" type="java.lang.Double">
<column name="addfee" precision="14" />
</property>
<property name="reducefee" type="java.lang.Double">
<column name="reducefee" precision="14" />
</property>
<property name="genTime" type="java.util.Date">
<column name="gen_time" length="23" not-null="true" />
</property>
</class>
</hibernate-mapping>
package com.acidpie.bean;
import java.util.Date;
/**
* OwnerServerfee generated by MyEclipse - Hibernate Tools
*/
public class OwnerServerfee implements java.io.Serializable {
// Fields
private OwnerServerfeeId id;
private String areaName;
private Double incomefee;
private Double addfee;
private Double reducefee;
private Date genTime;
// Constructors
/** default constructor */
public OwnerServerfee() {
}
/** minimal constructor */
public OwnerServerfee(OwnerServerfeeId id, Double incomefee, Date genTime) {
this.id = id;
this.incomefee = incomefee;
this.genTime = genTime;
}
/** full constructor */
public OwnerServerfee(OwnerServerfeeId id, String areaName, Double incomefee, Double addfee, Double reducefee, Date genTime) {
this.id = id;
this.areaName = areaName;
this.incomefee = incomefee;
this.addfee = addfee;
this.reducefee = reducefee;
this.genTime = genTime;
}
// Property accessors
public OwnerServerfeeId getId() {
return this.id;
}
public void setId(OwnerServerfeeId id) {
this.id = id;
}
public String getAreaName() {
return this.areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public Double getIncomefee() {
return this.incomefee;
}
public void setIncomefee(Double incomefee) {
this.incomefee = incomefee;
}
public Double getAddfee() {
return this.addfee;
}
public void setAddfee(Double addfee) {
this.addfee = addfee;
}
public Double getReducefee() {
return this.reducefee;
}
public void setReducefee(Double reducefee) {
this.reducefee = reducefee;
}
public Date getGenTime() {
return this.genTime;
}
public void setGenTime(Date genTime) {
this.genTime = genTime;
}
}
package com.acidpie.bean;
/**
* OwnerServerfeeId generated by MyEclipse - Hibernate Tools
*/
public class OwnerServerfeeId implements java.io.Serializable {
// Fields
private String lsName;
private String ownerNo;
private String truemonth;
// Constructors
/** default constructor */
public OwnerServerfeeId() {
}
/** full constructor */
public OwnerServerfeeId(String lsName, String ownerNo, String truemonth) {
this.lsName = lsName;
this.ownerNo = ownerNo;
this.truemonth = truemonth;
}
// Property accessors
public String getlsName() {
return this.lsName;
}
public void setlsName(String lsName) {
this.lsName = lsName;
}
public String getOwnerNo() {
return this.ownerNo;
}
public void setOwnerNo(String ownerNo) {
this.ownerNo = ownerNo;
}
public String getTruemonth() {
return this.truemonth;
}
public void setTruemonth(String truemonth) {
this.truemonth = truemonth;
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof OwnerServerfeeId) ) return false;
OwnerServerfeeId castOther = ( OwnerServerfeeId ) other;
return ( (this.getlsName()==castOther.getlsName()) || ( this.getlsName()!=null && castOther.getlsName()!=null && this.getlsName().equals(castOther.getlsName()) ) )
&& ( (this.getOwnerNo()==castOther.getOwnerNo()) || ( this.getOwnerNo()!=null && castOther.getOwnerNo()!=null && this.getOwnerNo().equals(castOther.getOwnerNo()) ) )
&& ( (this.getTruemonth()==castOther.getTruemonth()) || ( this.getTruemonth()!=null && castOther.getTruemonth()!=null && this.getTruemonth().equals(castOther.getTruemonth()) ) );
}
public int hashCode() {
int result = 17;
result = 37 * result + ( getlsName() == null ? 0 : this.getlsName().hashCode() );
result = 37 * result + ( getOwnerNo() == null ? 0 : this.getOwnerNo().hashCode() );
result = 37 * result + ( getTruemonth() == null ? 0 : this.getTruemonth().hashCode() );
return result;
}
}
现有报表系统对该表进行查询的报表中SQL语句如下
select owner_no,area_name,sum(incomefee),sum(isnull(addfee,0)-isnull(reducefee,0)) from owner_serverfee where truemonth=:month group by owner_no,area_name
我用以下代码
Query qry=session.createQuery("select ownerNo,areaName,sum(incomefee),sum(isnull(addfee,0)-isnull(reducefee,0)) from OwnerServerfee where trueMonth=:month group by ownerNo,areaName");
qry.setString("month","200611");
List result = qry.list();
会出现以下异常
Exception in thread "main" java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.IdentNode
+-[IDENT] IdentNode: 'ownerNo' {originalText=ownerNo}
at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:140)
at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:702)
at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:531)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:645)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at com.acidpie.business.Test.main(Test.java:32)
我个人分析是因为在针对owner_no,truemonth字段的get和set函数是在OwnerServerfeeId类中定义,而OwnerServerfeeId并没有映射文件。
但是我想了三天也没有解决如何通过hibernate来实现这个报表,如果还不行我只有用JDBC来实现了。
各位牛人!