Hibernate SQL异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围

时间:2014-06-24 07:33:10

标签: java hibernate

我正在编写以下查询以获取今天日期的记录

Session session = getSessionFactory().getCurrentSession();
DateTime todayAtMidnight = new DateTime().withTimeAtStartOfDay();
DateTime tomorrowAtMidnight = todayAtMidnight.plusDays(1);
Query query = session.getNamedQuery("findUsersOrder")
    .setTimestamp("todayAtMidnight", todayAtMidnight.toDate())
    .setTimestamp("tomorrowAtMidnight", tomorrowAtMidnight.toDate());
List<GroupTweetOrder> usersOrder= query.list();

并在Java类中为他的表

@NamedQueries({
    @NamedQuery(name = "findUsersOrder", query = "FROM GroupTweetOrder WHERE gtoAddedDate >= :todayAtMidnight and gtoAddedDate < :tomorrowAtMidnight")
})

但是当我试图运行这个HSQL时,我正在

Jun 23, 2014 5:13:11 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=formcontract:iconOnly, Message=java.lang.StringIndexOutOfBoundsException: String index out of range: 0
Jun 23, 2014 5:13:11 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
javax.faces.event.AbortProcessingException: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145)
    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:123)
    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:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    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:722)

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:658)
    at org.hibernate.type.descriptor.java.CharacterTypeDescriptor.wrap(CharacterTypeDescriptor.java:79)
    at org.hibernate.type.descriptor.java.CharacterTypeDescriptor.wrap(CharacterTypeDescriptor.java:33)
    at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$2.doExtract(VarcharTypeDescriptor.java:66)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:65)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:269)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:265)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:357)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2701)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1541)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1473)
    at org.hibernate.loader.Loader.getRow(Loader.java:1373)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640)
    at org.hibernate.loader.Loader.doQuery(Loader.java:850)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
    at org.hibernate.loader.Loader.doList(Loader.java:2447)
    at org.hibernate.loader.Loader.doList(Loader.java:2433)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263)
    at org.hibernate.loader.Loader.list(Loader.java:2258)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1161)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at com.ccc.spring.dao.GroupDistDAO.fetchTodayGroupOrder(GroupDistDAO.java:82)
    at com.ccc.spring.service.GroupDistService.fetchTodayGroupOrder(GroupDistService.java:40)
    at com.ccc.spring.service.GroupDistService$$FastClassBySpringCGLIB$$a552f952.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
    at com.ccc.spring.service.GroupDistService$$EnhancerBySpringCGLIB$$647c04d.fetchTodayGroupOrder(<generated>)
    at com.ccc.managedController.TweetDistributionManagedBean.sendGroupTweets(TweetDistributionManagedBean.java:175)
    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:601)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
    ... 31 more

3 个答案:

答案 0 :(得分:7)

当表中的空varchar列加载到char或Character字段时,Hibernate中的此错误显然会发生。当我在Java中将字段定义为char,在SQL中为“varchar(10)”时,我收到了同样的错误,表中的数据是空字符串('')。只要表数据是单个字符,它就可以正常工作。 (运行Hibernate 3.6.10)

有关失败代码的说明,请参阅https://forum.hibernate.org/viewtopic.php?f=1&t=1008581

答案 1 :(得分:0)

此问题不是由于日期而发生....这个问题是由于我使用char作为数据类型的表中的字符字段所以我将char数据类型更改为{{ 1}}现在这很好用

答案 2 :(得分:-3)

根据Java SE 7 Doc

Thrown by String methods to indicate that an index is either negative or greater than
the size of the string. For some methods such as the charAt method, this exception 
also is thrown when the index is equal to the size of the string.

请在此处查看more