- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Hibernate 和 HQL 查询来获取行数。我写了下面的代码:
public long countTexteEstALOrdreDuJour(String examenId) throws DAOException {
StringBuilder hqlQuery = new StringBuilder();
hqlQuery.append("SELECT count(*) ")
.append("FROM Examen as e ")
.append("JOIN e.organeSaisi as os ")
.append("JOIN os.texteDeLoi as t ")
.append("WHERE e.organeSaisi = some (SELECT ex.organeSaisi FROM Examen as ex WHERE ex.id = :examenId) ")
.append("AND e.examenEstALOrdreDuJour = 1 ");
try {
Query query = this.getSession().createQuery(hqlQuery.toString());
query.setParameter("examenId", examenId);
String result = (String)query.uniqueResult();
Long ret = null;
if (result==null) {
ret = 0l;
} else {
ret = Long.parseLong(result);
}
return ret;
} catch (JDBCException e) {
throw new DAOException(String.valueOf(e.getErrorCode()), e);
} catch (HibernateException e) {
throw new DAOException("HibernateException ", e);
}
}
当我调用该方法时,出现异常:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:36)
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:52)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:91)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:283)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:278)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:578)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1716)
at org.hibernate.loader.Loader.doQuery(Loader.java:801)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2542)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:459)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:890)
at com.an.eloi.model.dao.texte.TexteDAO.countTexteEstALOrdreDuJour(TexteDAO.java:247)
at com.an.eloi.model.domaine.texte.Texte.countTexteEstALOrdreDuJour(Texte.java:169)
at com.an.eloi.service.transactionnel.texte.TexteService.countTexteEstALOrdreDuJour(TexteService.java:348)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy39.countTexteEstALOrdreDuJour(Unknown Source)
at com.an.eloi.facade.texte.TexteFacade.countTexteEstALOrdreDuJour(TexteFacade.java:193)
at com.an.eloi.web.controller.texte.TexteController.updateTexte(TexteController.java:353)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:743)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:672)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:82)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.an.eloi.common.EloiActionFilter.doHttpFilter(EloiActionFilter.java:110)
at com.an.eloi.common.EloiActionFilter.doHttpFilterSynchronizedBySession(EloiActionFilter.java:158)
at com.an.eloi.common.EloiActionFilter.doFilter(EloiActionFilter.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.an.fwk.core.web.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:84)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
发生了什么事?
最佳答案
很可能在您的 Examen
实体中,id
字段是一个数字 Long
并且应该这样设置。将 examenId
类型更改为 Long
或在设置查询参数值时解析它:
Query query = this.getSession().createQuery(hqlQuery.toString());
query.setParameter("examenId", Long.parseLong(examenId));
关于java - 调用 Query.uniqueResult() 时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724143/
我已将 Hibernate 更新到 5.2.2 版本,并且不推荐使用 uniqueResult() 方法。我正在阅读文档,但找不到应该使用哪种方法来替换它?提前致谢!! 最佳答案 我也发现了同样的问题
我有一个使用 hibernate uniqueResult 方法的登录 Controller 。当我在 eclipse 的 tomcat 服务器上测试时,一切正常。但是当我将我的 webapps 部署
我有以下代码: import com.querydsl.jpa.impl.JPAQuery; ... QCustomer customer = QCustomer.customer; JPAQuery
早上好 我正在尝试以下操作: 在更新对象之前,我想从最后一条记录中检索一个字段以将其添加到新行中。所以这就是我所做的 int idLuce = Integer.parseInt(msg); Ill
我需要知道 uniqueResult() 是否返回一个新对象,换句话说,是否在内部生成一个 malloc()?因为当我比较对象的内存地址时我的测试失败了。 sameObjectButRetrieved
我有一个包含名称和创建日期的 USERS 表,以及一个 api 函数 T read(Criterion... criteria) 按我无法更改的标准进行搜索。 我的问题是该函数返回 crit.uniq
我正在学习 hibernate ,我正在尝试查询表以查找具有特定 ID 的人。当我调用 criteria.unique 结果时,没有抛出任何错误,但它返回一个空值,任何人都可以帮我弄清楚这是怎么回事吗
我正在尝试使用 Hibernate 和 HQL 查询来获取行数。我写了下面的代码: public long countTexteEstALOrdreDuJour(String examenId) th
我正在学习 Mockito,我想要模拟方法 doubleCountAnimal(Session) 中使用的 Hibernate session 和 Hibernate 标准: public class
当通过 hibernate 获取多行时,我使用下面的逻辑来读取每一行条目。使用 Hibernate 4 和 Websphere 7 @SuppressWarnings("unchecked") Lis
谁能告诉我这段代码有什么区别: // This following method checks if there is an open session // and if yes - returns
正在写作 session.createCriteria(Person.class) .add(Restrictions.eq("id", personId)) .setMaxResults(1) .u
TLDR: The following code is run in the different databases, Oracle: select sysdate from dual SQLite
我是一名优秀的程序员,十分优秀!