[英]Exception using jTable and Hibernate on Paging & Sorting
我收到以下异常:
org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: org.postgresql.util.PSQLException: ERREUR: constante non entière dans ORDER BY
Position: 222
我在GareDAOIpml中有一个方法来显示数据库中的所有行,我的方法show()是:
public List<Gare> show(int startIndex, int pageSize, String jtSorting) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Gare> gares = null;
try {
Query q = session.createQuery("from Gare ORDER BY "+jtSorting+" ");
q.setFirstResult(startIndex);
q.setMaxResults(pageSize);
gares = (List<Gare>)q.list();
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return gares;
}
<script type="text/javascript">
$(document).ready(function() {
$('#StudentTableContainer').jtable({
title : 'liste des gares',
paging: true,
pageSize: 10,
sorting: false,
actions : {
listAction : 'listergare',
updateAction: 'updategare',
deleteAction: 'deletegare'
},
fields : {
idgare : {
title : 'identifiant de la gare',
key : true,
list : false,
edit : false
},
usergare : {
title : 'utilisateur de la gare',
width : '25%',
list : true,
edit : true
},
ville : {
title : 'ville ',
width : '25%',
type: 'date',
list : true,
edit : true
}
}
});
$('#StudentTableContainer').jtable('load');
});
根据[PostgreSQL]中的 David J.在ORDER BY中的非整数常量:确切地说,为什么? -Grokbase ,在以下情况下会发生错误:
一种可能的情况是,用户打算使用双引号来指定标识符,而使用单引号。 由于文字常量不会影响排序顺序,因此计划人员应静默丢弃它或引发异常。 首选例外是因为常量文字的存在可能意味着查询生成的任何内容都将被破坏并且应予以修复。
检查查询中的ORDER BY子句。
另一方面,您可以启用Hibernate将所有生成的SQL语句记录到控制台的功能。 在Hibernate配置文件hibernate.cfg.xml
添加下一个属性。
<prop key="hibernate.show_sql">true</prop>
另请参见使用Hibernate时如何打印带有参数值的查询字符串 。
UPDATE
您需要在HQL字符串中使用别名 :
session.createQuery("SELECT g FROM Gare g" +
(jtSorting != null ? " ORDER BY g." + jtSorting : ""));
请参阅第14章中的更多内容。HQL:Hibernate查询语言
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.