浏览 8676 次
锁定老帖子 主题:一个用hibernate做报表的问题
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-03-24
表结构: 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来实现了。 各位牛人! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-03-25
你的OwnerServerfee中没有ownerNo字段当然会出错,应用连接查询,感觉映射关系没理清. 另复杂的查询可直接用jdbc
|
|
返回顶楼 | |
发表时间:2007-03-25
hibernate确实不适合做复杂的报表,如果是为了学习的目的,建议不要用报表系统上手,概念上不是特别吻合。
ps: composite-id 最好也别用,图增烦恼的玩意儿。 |
|
返回顶楼 | |
发表时间:2007-03-25
谁能帮我解决一下阿,我可不想出师不利阿
|
|
返回顶楼 | |
发表时间:2007-03-25
查询语句有问题 请用 id.ownerNo 因为你是用组合主键
select id.ownerNo,areaName,sum(incomefee),sum(isnull(addfee,0)-isnull(reducefee,0)) from OwnerServerfee where trueMonth=:month group by ownerNo,areaName |
|
返回顶楼 | |
发表时间:2007-03-29
|
|
返回顶楼 | |
发表时间:2007-05-19
No data type for node这个错误是因为你在hql欲查询的字段名(对象属性名)和hbm.xml中的属性不匹配或者找不到引起的。
hql中属性名必须和pojo中的属性名一致(区分大小写)。 |
|
返回顶楼 | |