java.lang.ClassCastException:java.lang.String无法强制转换为java.util.Calendar

时间:2014-04-06 16:26:32

标签: java hibernate casting calendar

我正在尝试从String转换为Calendar,但总是提供异常。

这是我的代码: -

public List<ClassA> getData(String fromDateTime, String toDateTime, Integer apiId)
            throws DAOException, ParseException {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cal1.setTime(df.parse(fromDateTime));
        cal2.setTime(df.parse(toDateTime));
return findByCriteria(Restrictions.eq("apiId", apiId),
                Restrictions.between("searchGenerationTime", fromDateTime, toDateTime));       
}
请有人告诉我这段代码我错在哪里? 让我们假设来自日期和时间toDateTime是用户输入2014-04-02 19:24:43&amp; 2014-04-03 12:03:51分别。 感谢。

编辑: - 这是我的堆栈跟踪

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Calendar
    at org.hibernate.type.descriptor.java.CalendarTypeDescriptor.unwrap(CalendarTypeDescriptor.java:40)
    at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$1.doBind(TimestampTypeDescriptor.java:53)
    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.loader.Loader.bindPositionalParameters(Loader.java:1873)
    at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1844)
    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.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
    at com.via.database.util.DAOUtil.findByCriteriaInternal(DAOUtil.java:227)
    at com.via.database.util.DAOUtil.findByCriteria(DAOUtil.java:198)
    at com.via.database.dao.hibernate.GenericHibernateDAO.findByCriteria(GenericHibernateDAO.java:175)
    at com.eos.accounts.database.dao.hibernate.HotelSearcherRequestResponseDao.getData(HotelSearcherRequestResponseDao.java:53)
    at com.eos.hotels.HotelSearcherUtility.getHotelSearchRequestResopnseData(HotelSearcherUtility.java:50)
    at com.eos.b2c.ui.B2cCallcenterNavigation.doGetExtension(B2cCallcenterNavigation.java:9568)
    at com.eos.b2c.ui.B2cCallcenterNavigation.doGet(B2cCallcenterNavigation.java:5851)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.eos.accounts.UserManagerFilter.doFilter(UserManagerFilter.java:972)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:701)

2 个答案:

答案 0 :(得分:0)

我认为return语句应该如下:

return findByCriteria(Restrictions.eq("apiId", apiId),
               Restrictions.between("searchGenerationTime", cal1, cal2)); 

答案 1 :(得分:0)

在分析异常堆栈跟踪之后,我发现实体ClassA有一个Calendar字段searchGenerationTime映射到SQL时间戳列。

对于这种情况,您只能传递这些类的参数:

  • java.util.Calendar中
  • java.sql.Date
  • 的java.sql.Time
  • 的java.sql.Timestamp
  • java.util.Date

您可以在hibernate source code中看到这一点。

事实上,Hibernate从哪里知道你的日期格式(“yyyy-MM-dd HH:mm:ss”)?