hibernatesqlHibernate中createQuery与createSQLQuery两者的区别

最近应用开发的过程中出现了一个小问题,顺便记录一下原因和方法--hibernatesql

    

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

    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() + "'" ;
}
return whereSql;
 
}

    

    

    应该改成这样的,

    

    每日一道理
听,是谁的琴声,如此凄凉,低调的音,缓慢的节奏,仿佛正诉说着什么。音低调得略微有些抖动,听起来似乎心也有些抖动,我感觉到一种压抑的沉闷气息,是否已凝结在这空气中……

    

Java code
 
?
1
2
3
4
return Integer.valueOf(session.createQuery(
"select count(alias) from " + clazz.getName()
+ " as alias " +whereSql).uniqueResult()
+ "" );

    

    

    

    

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() + "'" ;
}
return whereSql;
 
}

上面是从网上找的很有效:

createQuery与createSQLQuery区分

前者用的hql语句进行查询,后者可以用sql语句查询
前者以hibernate生成的Bean为对象装入list返回
后者则是以对象数组进行存储
所以应用createSQLQuery有时候也想以hibernate生成的Bean为对象装入list返回,就不是很方便
突然发明createSQLQuery有这样一个方法可以直接转换对象
Query query = session.createSQLQuery(sql).addEntity(XXXXXXX.class);
XXXXXXX 代表以hibernate生成的Bean的对象,也就是数据表映射出的Bean。


    

文章结束给大家分享下程序员的一些笑话语录: 关于编程语言
如果 C++是一把锤子的话,那么编程就会变成大手指头。
如果你找了一百万只猴子来敲打一百万个键盘,那么会有一只猴子会敲出一 段 Java 程序,而其余的只会敲出 Perl 程序。
一阵急促的敲门声,“谁啊!”,过了 5 分钟,门外传来“Java”。
如果说 Java 很不错是因为它可以运行在所有的操作系统上,那么就可以说 肛交很不错,因为其可以使用于所有的性别上。

--------------------------------- 原创文章 By
hibernate和sql
---------------------------------

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3108947.html