读书人

org.hibernate.exception.SQLGrammarE

发布时间: 2011-11-25 21:31:10 作者: rapoo

[求助] 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

<?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.Inquire" table="t_inquire" >        <id name="id" type="java.lang.Integer">            <column name="id" />            <generator class="increment" />        </id>        <property name="name" type="java.lang.String">            <column name="name" length="30" />        </property>           </class></hibernate-mapping>


[解决办法]
你这个表t_quotation什么结构 ?里面可有inquire_id这个字段
[解决办法]
hql语句查询试试
String hql = "select id, product, model, brand, encap, postil, quantity, " +
"price from Quotation where Inquire.id in (select id from Inquire " +
"where name = 'fff') ";

读书人网 >J2EE开发

热点推荐