Hibernate查询不适用于将值作为绑定参数(?),但在置于查询中时有效

时间:2019-11-23 02:29:24

标签: java hibernate jpa hql

我的问题是:为什么以下查询有效?

String hql = "select sum(total) from Sale s where s.customerType = 'LOYAL' and s.customer.id = 4";
Query query = session.createQuery(hql);
System.out.println(query.getSingleResult()); // prints 1500.0 (exact value)

在上面的查询中,Hibernate执行以下SQL(可以):

select
        sum(sale0_.total) as col_0_0_ 
    from
        sales sale0_ 
    where
        sale0_.customer_type='LOYAL' 
        and sale0_.customer_id=4

但是,当我尝试进行以下更改的第一个代码段时,它将引发异常。

String hql = "select sum(total) from Sale s where s.customerType = 'LOYAL' and s.customer.id = ?";
Query query = session.createQuery(hql);
query.setParameter(1, 4);
System.out.println(query.getSingleResult()); // It doesn't work!

例外:(这不是完整的日志,只是部分日志,但我认为足够了)

DEBUG org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl - JDBC transaction marked for rollback-only (exception provided for stack trace)
java.lang.Exception: exception just for purpose of providing stack trace
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.markRollbackOnly(JdbcResourceLocalTransactionCoordinatorImpl.java:324) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at org.hibernate.engine.transaction.internal.TransactionImpl.markRollbackOnly(TransactionImpl.java:203) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.markForRollbackOnly(AbstractSharedSessionContract.java:392) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:717) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:109) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at com.karyal.shmeryal.model.Sale.lambda$getSumOfTotalNotCashFor$1(Sale.java:309) ~[Shmeryal/:?]
    at com.karyal.shmeryal.support.util.DBUtil.exec(DBUtil.java:53) ~[Shmeryal/:?]
    at com.karyal.shmeryal.model.Sale.getSumOfTotalNotCashFor(Sale.java:307) ~[Shmeryal/:?]
    at com.karyal.shmeryal.Shmeryal.test(Shmeryal.java:94) ~[Shmeryal/:?]
    at com.karyal.shmeryal.Shmeryal.start(Shmeryal.java:23) ~[Shmeryal/:?]
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863) ~[jfxrt.jar:?]
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326) ~[jfxrt.jar:?]
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295) ~[jfxrt.jar:?]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_201]
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294) ~[jfxrt.jar:?]
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) ~[jfxrt.jar:?]
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) ~[jfxrt.jar:?]
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177) ~[jfxrt.jar:?]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_201]
DEBUG org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl - JDBC transaction marked for rollback-only (exception provided for stack trace)
java.lang.Exception: exception just for purpose of providing stack trace
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.markRollbackOnly(JdbcResourceLocalTransactionCoordinatorImpl.java:324) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at org.hibernate.engine.transaction.internal.TransactionImpl.markRollbackOnly(TransactionImpl.java:203) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.markForRollbackOnly(AbstractSharedSessionContract.java:392) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:717) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:109) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
    at com.karyal.shmeryal.model.Sale.lambda$getSumOfTotalNotCashFor$1(Sale.java:309) ~[Shmeryal/:?]
    at com.karyal.shmeryal.support.util.DBUtil.exec(DBUtil.java:53) ~[Shmeryal/:?]
    at com.karyal.shmeryal.model.Sale.getSumOfTotalNotCashFor(Sale.java:307) ~[Shmeryal/:?]
    at com.karyal.shmeryal.Shmeryal.test(Shmeryal.java:94) ~[Shmeryal/:?]
    at com.karyal.shmeryal.Shmeryal.start(Shmeryal.java:23) ~[Shmeryal/:?]
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863) ~[jfxrt.jar:?]
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326) ~[jfxrt.jar:?]
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295) ~[jfxrt.jar:?]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_201]
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294) ~[jfxrt.jar:?]
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) ~[jfxrt.jar:?]
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) ~[jfxrt.jar:?]
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177) ~[jfxrt.jar:?]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_201]
Exception in Application start method

注意::我正在使用Hibernate org.hibernate:hibernate-hikaricp:5.4.9.Final

1 个答案:

答案 0 :(得分:2)

Query query = session.createQuery("select sum(total) from Sale s where s.customerType = 'LOYAL' and s.customer.id = :customer_id");
query.setParameter("customer_id", 4L);
System.out.println("Sum of total from sale is : " + query.list().get(0));