gpt4 book ai didi

java - Hibernate 查询不适用于将值作为绑定(bind)参数 (?) 但在放入查询时有效

转载 作者:行者123 更新时间:2023-12-04 23:02:50 25 4
gpt4 key购买 nike

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

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!

异常(exception):(这不是整个日志,只是部分日志,但我认为就足够了)
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

最佳答案

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));

关于java - Hibernate 查询不适用于将值作为绑定(bind)参数 (?) 但在放入查询时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com