现在的位置: 首页 > 综合 > 正文

Hibernate中createQuery与createSQLQuery两者的区别

2012年10月28日 ⁄ 综合 ⁄ 共 1510字 ⁄ 字号 评论关闭

 最近几天在写项目,遇到了一个错:

Struts has detected an unhandled exception:
Messages: 

    ORA-00933: SQL ???????
    could not execute query
    could not execute query; SQL [ select count(*) from www.csdn.weibo.domain.Pictures where 1=1 and picture_type='aaa']; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

File:  oracle/jdbc/driver/DatabaseError.java
Line number:  112
Stacktraces
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [ select count(*) from www.csdn.weibo.domain.Pictures where 1=1 and picture_type='aaa']; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

在论坛发贴了,看到大家的回复我也改了改,还是没查询不到,于是乎请教老师吧,老师上来就看出问题了,唉唉,并且要我细看hibernate ,createQuery与createSQLQuery两者的区别,所以我先把我的错误展示给大家,然后再看他们的区别。

终于找到问题了 原来是把createQuery和createSQLQuery弄混了,在baseDaoImpl.java 里边我原来写的是

Java code

 

?

1
2
3
return 
Integer.valueOf((session.createSQLQuery(
                        
" select count(*) from " 
+ clazz.getName() + 
" "
                                
+ whereSql).uniqueResult() + 
""
));


在jsp中拼接sql语句中写的是

Java code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private 
String spliceSql() {
        
String whereSql = 
" where 1=1 "
;
 
        
if 
(user != 
null 
&& user.getId() != 
null 
&& !
""
.equals(user.getId())) {
            
whereSql += 
" and user_id=" 
+ user.getId() + 
" "
;
        
}
 
        
if 
(pictures != 
null 
&& pictures.getType() != 
null
                
&& !
""
.equals(pictures.getType())) {
            
whereSql += 
" and picture_type='" 
+ pictures.getType() + 
"'"

抱歉!评论已关闭.