求解答,在本地sql语句正确,在hibernate上就报错
public static void main(String[] args) throws Exception {ApplicationContext ac = new FileSystemXmlApplicationContext(new String[]{
"classpath:config/spring/applicationContext.xml",
"classpath:config/spring/applicationContext-cart.xml",
"classpath:config/spring/applicationContext-main.xml",
"classpath:config/spring/applicationContext-order.xml",
"classpath:config/spring/applicationContext-user.xml"
});
ProductDAO dao = (ProductDAO) ac.getBean("productDAOImpl");
List<Product> list = dao.findHotProduct_Books();
System.out.println(list);
}
}
运行的方法
public List<Product> findHotProduct_Books() throws Exception {
return (List<Product>) this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
return session.createSQLQuery("select dp.* spn from d_item di join d_product dp " +
"on(di.product_id=dp.id) group by product_id order by sum(di.product_num) desc")
.addEntity(Product.class).addScalar("sum",Hibernate.INTEGER).list();
}
});
}
配置文件
<hibernate-mapping>
<class name="org.tarena.dang.entity.Product" table="d_product" catalog="dangdang">
<id name="id" type="integer">
<column name="id" />
<generator class="identity"></generator>
</id>
<property name="productName" type="string">
<column name="product_name" length="100" not-null="true" />
</property>
<property name="description" type="string">
<column name="description" length="100" />
</property>
<property name="addTime" type="long">
<column name="add_time" />
</property>
<property name="fixedPrice" type="double">
<column name="fixed_price" precision="22" scale="0" not-null="true" />
</property>
<property name="dangPrice" type="double">
<column name="dang_price" precision="22" scale="0" not-null="true" />
</property>
<property name="keywords" type="string">
<column name="keywords" length="200" />
</property>
<property name="hasDeleted" type="integer">
<column name="has_deleted" not-null="true" />
</property>
<property name="productPic" type="string">
<column name="product_pic" length="200" />
</property>
<set name="cats" table="d_category_product" >
<key column="product_id"></key>
<many-to-many class="org.tarena.dang.entity.Category" column="cat_id"></many-to-many>
</set>
<!-- Book继承映射 -->
<joined-subclass name="org.tarena.dang.entity.Book" table="d_book">
<key column="id"></key>
<property name="author" type="string">
<column name="author" length="200" not-null="true" />
</property>
<property name="publishing" type="string">
<column name="publishing" length="200" not-null="true" />
</property>
<property name="publish_time" type="long">
<column name="publish_time" not-null="true" />
</property>
<property name="word_number" type="string">
<column name="word_number" length="15" />
</property>
<property name="which_edtion" type="string">
<column name="which_edtion" length="15" />
</property>
<property name="total_page" type="string">
<column name="total_page" length="15" />
</property>
<property name="print_time" type="long">
<column name="print_time" />
</property>
<property name="print_number" type="string">
<column name="print_number" length="15" />
</property>
<property name="isbn" type="string">
<column name="isbn" length="25" />
</property>
<property name="author_summary" type="string">
<column name="author_summary" length="10000" not-null="true" />
</property>
<property name="catalogue" type="string">
<column name="catalogue" length="10000" not-null="true" />
</property>
</joined-subclass>
</class>
控制台输出信息
Hibernate:
select
dp.* spn
from
d_item di
join
d_product dp
on(
di.product_id=dp.id
)
group by
product_id
order by
sum(di.product_num) desc
INFO - could not read column value from result set: clazz_; Column 'clazz_' not found.
WARN - SQL Error: 0, SQLState: S0022
ERROR - Column 'clazz_' not found.
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
at org.tarena.dang.dao.impl.ProductDAOImpl.findHotProduct_Books(ProductDAOImpl.java:107)
at org.tarena.dang.dao.impl.ProductDAOImpl.main(ProductDAOImpl.java:54)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2216)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at org.tarena.dang.dao.impl.ProductDAOImpl$5.doInHibernate(ProductDAOImpl.java:114)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
... 3 more
Caused by: java.sql.SQLException: Column 'clazz_' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2794) hibernate sql string exception --------------------编程问答-------------------- select dp.* spn,spn这是别名,还是对象的属性?是别名的话不对,应该是select dp.* as spn,这样也不对,不可能把所有的属性都付给spn,是对象也不对,应该是select dp.*,spn --------------------编程问答-------------------- 但我在本地能查询出来啊 --------------------编程问答-------------------- sql查询出的结果集没有clazz_这一列,hibernate无法把查询结果set给Product对象。 --------------------编程问答-------------------- 我product映射文件应该没错吧,我其他方法调用都能成功,就这个不行,好像说是hibernate不支持sum函数还是怎么的,不知怎么解决,求大神!!! --------------------编程问答-------------------- sum是数据库的关键字,改为别的试下。 --------------------编程问答-------------------- 我sql得这么写啊,改了就失去意义了 --------------------编程问答-------------------- Column 'clazz_' not found.
补充:Java , Web 开发