我数据库中有一个表:KT
我的POJO类如下:
KT.javapackage cn.nit.hibernate;import java.util.Date;public class KT { private int ktNo;
private String ktName;
private Date beginDate;
private Date endDate;
private Date actEndDate;
private Date sbDate;
private int amt;
private String amtFrom;
private int schoolAmt;
private int reimburseAmt;
private boolean reimbursed;
private boolean approved;
private boolean award;
private boolean frozen;
private int typeNo;
private String description;
private int status;
public Date getActEndDate() {
return actEndDate;
}
public void setActEndDate(Date actEndDate) {
this.actEndDate = actEndDate;
}
public int getAmt() {
return amt;
}
public void setAmt(int amt) {
this.amt = amt;
}
public String getAmtFrom() {
return amtFrom;
}
public void setAmtFrom(String amtFrom) {
this.amtFrom = amtFrom;
}
public boolean isApproved() {
return approved;
}
public void setApproved(boolean approved) {
this.approved = approved;
}
public boolean isAward() {
return award;
}
public void setAward(boolean award) {
this.award = award;
}
public Date getBeginDate() {
return beginDate;
}
public void setBeginDate(Date beginDate) {
this.beginDate = beginDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public boolean isFrozen() {
return frozen;
}
public void setFrozen(boolean frozen) {
this.frozen = frozen;
}
public String getKtName() {
return ktName;
}
public void setKtName(String ktName) {
this.ktName = ktName;
}
public int getKtNo() {
return ktNo;
}
public void setKtNo(int ktNo) {
this.ktNo = ktNo;
}
public int getReimburseAmt() {
return reimburseAmt;
}
public void setReimburseAmt(int reimburseAmt) {
this.reimburseAmt = reimburseAmt;
}
public boolean isReimbursed() {
return reimbursed;
}
public void setReimbursed(boolean reimbursed) {
this.reimbursed = reimbursed;
}
public Date getSbDate() {
return sbDate;
}
public void setSbDate(Date sbDate) {
this.sbDate = sbDate;
}
public int getSchoolAmt() {
return schoolAmt;
}
public void setSchoolAmt(int schoolAmt) {
this.schoolAmt = schoolAmt;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getTypeNo() {
return typeNo;
}
public void setTypeNo(int typeNo) {
this.typeNo = typeNo;
}
}对应的映射文件KT.hbm.xml:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.nit.hibernate.KT" table="KT">
<id name="ktNo" column="KTNo">
               <generator class="native">
</id>
<property name="ktName"/>
<property name="beginDate"/>
<property name="endDate"/>
<property name="actEndDate"/>
<property name="sbDate"/>
<property name="amt"/>
<property name="amtFrom"/>
<property name="schoolAmt"/>
<property name="reimburseAmt"/>
<property name="reimbursed"/>
<property name="approved"/>
<property name="award"/>
<property name="frozen"/>
<property name="typeNo"/>
<property name="description"/>
<property name="status"/>

</class>
</hibernate-mapping>hibernate.cfg.xml文件内容如下:<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost;DatabaseName=GJS</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">kimliu</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

<property name="hibernate.show_sql">true</property>

<mapping resource="cn/nit/hibernate/KT.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我只量执行简单的从数据库查询出数据显示到页面上:
package cn.nit.action;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import cn.nit.dao.HibernateDAOBean;
import cn.nit.hibernate.KT;
import cn.nit.manage.Select;public class SelectAction{

Select select = new Select();

KT[] kt = null;

public String execute(){

String sql1 = select.getSearch();
String sql2 = select.getFromTime();
String sql3 = select.getToTime();
String sql4 = select.getName();

System.out.println("sql1 = " + sql1);
System.out.println("sql2 = " + sql2);
System.out.println("sql3 = " + sql3);
System.out.println("sql4 = " + sql4);

//String sql = "select * from KT as kt where kt.status = '" + "2" + "'" ;

//System.out.println("sql = " + sql);

HibernateDAOBean hDAOB = new HibernateDAOBean();

kt = new KT[hDAOB.doSelectUserInfoDataFromDB().length];
System.out.println("去执行sql语句....");
kt = hDAOB.doSelectUserInfoDataFromDB();

HttpServletRequest request = ServletActionContext.getRequest();

request.setAttribute("kt", kt);

return "success";
}
}写了个类HibernateDAOBean.java,负责连接数据库,并查询出数据,我只显示部分代码,因为字数不够.
String sql = "from KT as kt where kt.status = ?";
System.out.println("执行sql 语句...");
Query query = session.createQuery(sql);

query.setString(0, "2");
System.out.println("查询条件已添加...");
list = (List)query.list();//执行到这的时候出错了。
System.out.println("进行iterator....");
for(Iterator it = list.iterator(); it.hasNext();){
kt = new KT[list.size()];
kt =  (KT[]) it.next();
}我在页面上一提交,就出现下面的情况:sql1 = null
sql2 = null
sql3 = null
sql4 = null
读取方法....
执行sql 语句...
查询条件已添加...
Hibernate: select kt0_.KTNo as KTNo2_, kt0_.ktName as ktName2_, kt0_.beginDate as beginDate2_, kt0_.endDate as endDate2_, kt0_.actEndDate as actEndDate2_, kt0_.sbDate as sbDate2_, kt0_.amt as amt2_, kt0_.amtFrom as amtFrom2_, kt0_.schoolAmt as schoolAmt2_, kt0_.reimburseAmt as reimbur10_2_, kt0_.reimbursed as reimbursed2_, kt0_.approved as approved2_, kt0_.award as award2_, kt0_.frozen as frozen2_, kt0_.typeNo as typeNo2_, kt0_.description as descrip16_2_, kt0_.status as status2_ from KT kt0_ where kt0_.status=?
2009-10-16 16:48:58,937 - org.hibernate.util.JDBCExceptionReporter -0    [http-8080-3] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 207, SQLState: 42S22
2009-10-16 16:48:58,953 - org.hibernate.util.JDBCExceptionReporter -16   [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter  - 列名 'reimburseAmt' 无效。
2009-10-16 16:48:58,953 - org.hibernate.util.JDBCExceptionReporter -16   [http-8080-3] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 207, SQLState: 42S22
2009-10-16 16:48:58,953 - org.hibernate.util.JDBCExceptionReporter -16   [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter  - 列名 'reimbursed' 无效。
2009-10-16 16:48:58,968 - org.hibernate.util.JDBCExceptionReporter -31   [http-8080-3] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 207, SQLState: 42S22
2009-10-16 16:48:58,968 - org.hibernate.util.JDBCExceptionReporter -31   [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter  - 列名 'approved' 无效。
2009-10-16 16:48:58,968 - org.hibernate.util.JDBCExceptionReporter -31   [http-8080-3] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 207, SQLState: 42S22
2009-10-16 16:48:58,968 - org.hibernate.util.JDBCExceptionReporter -31   [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter  - 列名 'award' 无效。
2009-10-16 16:48:58,968 - org.hibernate.util.JDBCExceptionReporter -31   [http-8080-3] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 207, SQLState: 42S22
2009-10-16 16:48:58,968 - org.hibernate.util.JDBCExceptionReporter -31   [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter  - 列名 'frozen' 无效。
2009-10-16 16:48:58,968 - org.hibernate.util.JDBCExceptionReporter -31   [http-8080-3] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 207, SQLState: 42S22
2009-10-16 16:48:58,984 - org.hibernate.util.JDBCExceptionReporter -47   [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter  - 列名 'typeNo' 无效。
org.hibernate.exception.SQLGrammarException: could not execute query
....
         ....
2009-10-16 16:48:59,000 - org.hibernate.util.JDBCExceptionReporter -63   [http-8080-3] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Warning: 207, SQLState: 42S22
2009-10-16 16:48:59,000 - org.hibernate.util.JDBCExceptionReporter -63   [http-8080-3] WARN  org.hibernate.util.JDBCExceptionReporter  - Preparing the statement failed: 列名 'reimburseAmt' 无效。
2009-10-16 16:55:55 org.apache.catalina.core.StandardContext reload
信息: Reloading this Context has started从上面的情况下只能,sql 语句已经发出了:Hibernate: select kt0_.KTNo as KTNo2_, kt0_.ktName as ktName2_, kt0_.beginDate as beginDate2_, kt0_.endDate as endDate2_, kt0_.actEndDate as actEndDate2_, kt0_.sbDate as sbDate2_, kt0_.amt as amt2_, kt0_.amtFrom as amtFrom2_, kt0_.schoolAmt as schoolAmt2_, kt0_.reimburseAmt as reimbur10_2_, kt0_.reimbursed as reimbursed2_, kt0_.approved as approved2_, kt0_.award as award2_, kt0_.frozen as frozen2_, kt0_.typeNo as typeNo2_, kt0_.description as descrip16_2_, kt0_.status as status2_ from KT kt0_ where kt0_.status=?但是为什么会在我执行:
list = (List)query.list();出错?谢谢各位了。灌水的就别来了,我被这东西折腾了一下午了。心情很不爽了。

解决方案 »

  1.   

    嗯,我对了一下午了,就怕是这个错误,而且还对应了KT.java与KT.hbm.xml文件,看有没有没对应上的。
      

  2.   

    对应的映射文件KT.hbm.xml: 
      这个文件错误啊。~!!!
        你用Hibernian反向工程生产hbm.xml文件就好了。·!
     你这个文件的错误
      

  3.   

    <?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 Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.entity.Users" table="Users" schema="dbo" catalog="Mobile">
            <id name="uid" type="java.lang.Integer">
                <column name="uid" />
                <generator class="native" />
            </id>
            <property name="uname" type="java.lang.String">
                <column name="uname" length="20" not-null="true" />
            </property>
            <property name="upwd" type="java.lang.String">
                <column name="upwd" length="20" not-null="true" />
            </property>
            <property name="unickname" type="java.lang.String">
                <column name="unickname" length="20" not-null="true" />
            </property>
            <property name="upassport" type="java.lang.String">
                <column name="upassport" length="18" not-null="true" />
            </property>
            <property name="usex" type="java.lang.String">
                <column name="usex" length="2" not-null="true" />
            </property>
            <property name="uage" type="java.lang.Integer">
                <column name="uage" not-null="true" />
            </property>
            <property name="uprovince" type="java.lang.String">
                <column name="uprovince" length="50" not-null="true" />
            </property>
            <property name="ucity" type="java.lang.String">
                <column name="ucity" length="50" not-null="true" />
            </property>
            <property name="uaddress" type="java.lang.String">
                <column name="uaddress" length="50" not-null="true" />
            </property>
            <property name="upostalcode" type="java.lang.Long">
                <column name="upostalcode" precision="6" scale="0" not-null="true" />
            </property>
            <property name="utel" type="java.lang.String">
                <column name="utel" length="20" not-null="true" />
            </property>
            <property name="umail" type="java.lang.String">
                <column name="umail" length="20" not-null="true" />
            </property>
            <property name="uquestion" type="java.lang.String">
                <column name="uquestion" length="50" />
            </property>
            <property name="uanswer" type="java.lang.String">
                <column name="uanswer" length="20" />
            </property>
            <property name="uintergral" type="java.lang.Double">
                <column name="uintergral" scale="4" />
            </property>
            <set name="oldWareses" inverse="true" >
                <key>
                    <column name="ouid" not-null="true" />
                </key>
                <one-to-many class="com.entity.OldWares" />
            </set>
            <set name="bbses" inverse="true">
                <key>
                    <column name="bsuid" not-null="true" />
                </key>
                <one-to-many class="com.entity.Bbs" />
            </set>
            <set name="buyOrders" inverse="true" lazy="false">
                <key>
                    <column name="buid" not-null="true" />
                </key>
                <one-to-many class="com.entity.BuyOrder" />
            </set>
        </class>
    </hibernate-mapping>
     这个是我一个User类的映射文件
      

  4.   

    你先别用参数,直接给个具体值,试试 String sql = "from KT as kt where kt.status =2";
      

  5.   

    我发现报错的基本都是boolean类型的,你将实体bean里的isAA改为 getAA先试试
      

  6.   

    reimburseAmt,看看这个字段在数据库表里面是否是一样的名字
      

  7.   

    列名 'reimburseAmt' 无效。看看你的表中有没有这一列。
    btw,你的hibernate是什么版本的,hbm.xml文件的配置好奇怪:)
      

  8.   


    错哪了?我是手动添加hibernate的jar包,因为我对hibernate不熟悉,所以通过手动去添加,到底错哪了?
      

  9.   

    你的数据查询语句有问题,你把它输出以下。 既然用了hiber  有个find方法。在那里面连 。find("select * from KT as kt where kt.status =?",new Object{把参数放进去});就可以了。_
      

  10.   

    哎,问题被我解决了。因为在对POJO映射的时候我没有写上对应的数据库中表的字段名,可是是因为字段没有对应上,所以报了那个错。也就是把KT.hbm.xml文件改成:<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <class name="cn.nit.hibernate.KT" table="KT">
    <id name="ktNo" column="KTNo">
    <generator class="native"/>
    </id>
    <property name="ktName" column="KTName"/>
    <property name="beginDate" column="BeginDate"/>
    <property name="endDate" column="EndDate"/>
    <property name="actEndDate" column="ActEndDate"/>
    <property name="sbDate" column="sbDate"/>
    <property name="amt" column="Amt"/>
    <property name="amtFrom" column="AmtFrom"/>
    <property name="schoolAmt" column="SchoolAmt"/>
    <property name="reimburseAmt" column="ReimbursedAmt"/>
    <property name="reimbursed" column="isReimbursed"/>
    <property name="approved" column="isApproved"/>
    <property name="award" column="isAward"/>
    <property name="frozen" column="isFrozen"/>
    <property name="typeNo" column="KTTypeNo"/>
    <property name="description" column="Description"/>
    <property name="status" column="Status"/>

    </class>
    </hibernate-mapping>我怎么记得说不要写column,会自动查找相关字段,也许是要属性名与字段名?是不是这个原因?
      

  11.   

    这个问题很囧啊,都一天了,还是没解决,到底怎么会事儿,现在都有点怀疑,是不是hibernate 的bug啊。
    汗啊……
      

  12.   

    解决了,啊,配置都是对的,我把数据库重建了一下,pojo以及映射文件重新生成,再发布运行就可以了。我感觉好像跟hibernate的缓存机制有问题。