我的一个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 queryservice:
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: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 略
}
public class Inquire {
private Integer id;
private String name;//get set 略
}
<?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 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>
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 queryservice:
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: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 略
}
public class Inquire {
private Integer id;
private String name;//get set 略
}
<?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 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>
String hql = "select id, product, model, brand, encap, postil, quantity, " +
"price from Quotation where Inquire.id in (select id from Inquire " +
"where name = 'fff') ";
这样写也不行,但这样写就可以了"from Quotation where Inquire.id in (select id from Inquire " +
"where name = 'fff')"
String sql = "select t.id, t.product, t.model, t.brand, t.encap, t.postil, t.quantity, " +
"t.price from t_quotation t where inquire_id in (select id from t_inquire " +
"where name = 'fff') ";
SQLQuery sq = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
sq.addEntity("t",Quotation.class);
sq.list();
改成如下试试:
String sql = "select t.* from t_quotation t where t.inquire_id in (select id from t_inquire " + "where name = 'fff') ";
SQLQuery sq = this.hibernateTemplate.getSessionFactory().getCurrentSession().createSQLQuery(sql);
sq.addEntity("t",Quotation.class);
sq.list();原因是addEntity不支持任意列,只支持*符号