日期:2014-05-20  浏览次数:20743 次

[求助] hibernate sqlquery.addEntity 的问题
我的一个POJO类用SqlQuery后addEntity,好像不能按我select 回来的字段填写,必须是全部字段,将hibernate打印出的SQL直接到数据库里查询是正常的。
Hibernate: select id, product, model, brand, encap, postil, quantity, price from t_quotation where inquire_id in (select id from t_inquire where name = 'fff')
could not read column value from result set: inquire_id; Column 'inquire_id' not found.
SQL Error: 0, SQLState: S0022
Column 'inquire_id' not found.
org.hibernate.exception.SQLGrammarException: could not execute query

service:
 
Java code

String sql = "select id, product, model, brand, encap, postil, quantity, " +
    "price from t_quotation where inquire_id in (select id from t_inquire " +
                "where name = 'fff') ";
SQLQuery sq = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
        sq.addEntity(Quotation.class);
        sq.list();


pojo:
Java code

public class Quotation {
        private Integer id;
    private Inquire inquire;
    private String product;
    private String model;
    private String brand;
    private String encap;
    private String postil;
    private Integer quantity;
    private Double price;

//get set 略
}


Java code

public class Inquire {
        private Integer id;
    private String name;

//get set 略
}


XML code

<?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">
<hibernate-mapping>
    <class name="com.Quotation" table="t_quotation" >
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <many-to-one name="inquire" column="inquire_id" update="false"
            class="com.Inquire" lazy="false"
            not-found="ignore">
        </many-to-one>
        <property name="product" type="java.lang.String">
            <column name="product" length="30" />
        </property>
        <property name="model" type="java.lang.String">
            <column name="model" length="20" />
        </property>
        <property name="brand" type="java.lang.String">
            <column name="brand" length="20" />
        </property>
        <property name="encap" type="java.lang.String">
            <column name="encap" length="20" />
        </property>
        <property name="postil" type="java.lang.String">
            <column name="postil" length="20" />
        </property>
        <property name="quantity" type="java.lang.Integer">
            <column name="quantity" />
        </property>
        <property name="price" type="java.lang.Double">
            <column name="price" precision="10" scale="3" />
        </property>
    </class>
</hibernate-mapping>


XML code