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>
控制台输出信息
DEBUG -
select
dp.*
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
Hibernate:
select
dp.*
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
DEBUG - preparing statement
DEBUG - about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG - processing result set
DEBUG - result set row: 0
DEBUG - returning '11' as column: id
DEBUG - result row: EntityKey[org.tarena.dang.entity.Product#11]
INFO - could not read column value from result set: clazz_; Column 'clazz_' not found.
DEBUG - about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
DEBUG - could not execute query [select dp.* 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]
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)
at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:237)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1442)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1284)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
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)
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)
WARN - SQL Error: 0, SQLState: S0022
ERROR - Column 'clazz_' not found.
DEBUG - after autocommit
DEBUG - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG - after transaction completion
DEBUG - Closing Hibernate Session
DEBUG - closing session
DEBUG - performing cleanup
DEBUG - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG - after transaction completion
DEBUG - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG - after transaction completion
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
... 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)
at
hibernatesqlexception
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>
控制台输出信息
DEBUG -
select
dp.*
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
Hibernate:
select
dp.*
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
DEBUG - preparing statement
DEBUG - about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG - processing result set
DEBUG - result set row: 0
DEBUG - returning '11' as column: id
DEBUG - result row: EntityKey[org.tarena.dang.entity.Product#11]
INFO - could not read column value from result set: clazz_; Column 'clazz_' not found.
DEBUG - about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
DEBUG - could not execute query [select dp.* 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]
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)
at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:237)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1442)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1284)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
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)
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)
WARN - SQL Error: 0, SQLState: S0022
ERROR - Column 'clazz_' not found.
DEBUG - after autocommit
DEBUG - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG - after transaction completion
DEBUG - Closing Hibernate Session
DEBUG - closing session
DEBUG - performing cleanup
DEBUG - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG - after transaction completion
DEBUG - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG - after transaction completion
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
... 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)
at
hibernatesqlexception
解决方案 »
- 关于hibernate的catalog读取
- struts中如何在后台控制器实现前台的带有checkbox的删除操作,下面代码中省略号部分该如何操作?
- 求助 webservice 高手进……
- 各位高手请帮忙看下:"jsp1.jsp": java.lang.NoClassDefFoundError: org/apache/tools/ant/util/FileUtils错误
- AJAX的问题?
- J2EE的特点是什么?要详细的回答~谢谢!
- java编程思想里看到一段代码,不是很理解
- weblogic 8.0创建WINNT服务后,无法启动,出现以下错误提示,请各位大侠指点
- 关于hibernate的几个问题
- 求高手解答 java.lang.ClassCastException: org.quartz.simpl.SimpleThreadPool
- 使用Jquery 分页 拼接的<table> 里 接不到 Date 的值
- springMVC重定向找不到相应的视图页面
https://forum.hibernate.org/viewtopic.php?t=985124Column 'clazz_' 应该是hibernate本应该生成的中间产物,因为什么原因没有产生呢?
"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();你对查询的结果指定了别名,别名是spn,为什么你加的却是sum?