org.hibernate.exception.GenericJDBCException:无法执行查询] 根本原因

Posted

技术标签:

【中文标题】org.hibernate.exception.GenericJDBCException:无法执行查询] 根本原因【英文标题】:org.hibernate.exception.GenericJDBCException: could not execute query] with root cause 【发布时间】:2017-07-22 08:10:09 【问题描述】:

我不确定如何修复以下异常。我的数据源被注入到 sessionfactory 并被 DAO 用来创建查询。考虑到应用程序的其他模块运行良好,我的数据库运行良好。这让我怀疑我的模型类“购物”在某种程度上是错误的。在 mysql 数据库中使用 java.sql.blob 表示长 blob 的方式不正确吗?

SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/Online_Shopping] threw exception [Request processing failed; nested exception is org.springframework.orm.hibernate4.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [select this_.Name as Name1_0_0_, this_.Date1 as Date2_0_0_, this_.Date2 as Date3_0_0_, this_.Image as Image4_0_0_, this_.Price as Price5_0_0_ from shopping this_]; SQL state [S1009]; error code [0]; could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query] with root cause java.sql.SQLException: Value 'toy

模型类:

@Entity
    @Table(name="shopping")
    public class Shopping implements java.io.Serializable 
        private static final long serialVersionUID = 1L;
            @Id
        @Column(name="Name", unique=true, nullable=false, length=50)
    public String getName() 
            return name;
        
        public void setName(String name) 
            this.name = name;
        

        @Column(name="Price", nullable=false, length=50)
        public String getPrice() 
            return price;
        
        public void setPrice(String price) 
            this.price = price;
        
        @Temporal(TemporalType.DATE)
        @Column(name="Date1", nullable=false)
        public Date getDate1() 
            return date1;
        
        public void setDate1(Date date1) 
            this.date1 = date1;
        
        @Temporal(TemporalType.DATE)
        @Column(name="Date2", nullable=false)
        public Date getDate2() 
            return date2;
        
        public void setDate2(Date date2) 
            this.date2 = date2;
        
    private String name;
      private String price;
      private Date date1;
     private Date date2;
     private Blob image;
     @Column(name="Image", nullable=false)
    public Blob getImage() 
        return image;
    
    public void setImage(Blob image) 
        this.image = image;
    
    

DAO 方法:

public List<Shopping> getShopping() 
        Session session = sessionFactory.openSession();
        List<Shopping> list = session.createCriteria(Shopping.class).list();
        session.close();
        return list;
    

完整的堆栈跟踪:

EVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/Online_Shopping] threw exception [Request processing failed; nested exception is org.springframework.orm.hibernate4.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [select this_.Name as Name1_0_0_, this_.Date1 as Date2_0_0_, this_.Date2 as Date3_0_0_, this_.Image as Image4_0_0_, this_.Price as Price5_0_0_ from shopping this_]; SQL state [S1009];  q qerror code [0]; could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query] with root cause java.sql.SQLException: Value 'toy
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
            at com.mysql.jdbc.ResultSetRow.getDateFast(ResultSetRow.java:141)
            at com.mysql.jdbc.BufferRow.getDateFast(BufferRow.java:707)
            at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2206)
            at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2168)
            at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2233)
            at org.hibernate.type.descriptor.sql.DateTypeDescriptor$2.doExtract(DateTypeDescriptor.java:67)
            at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
            at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:265)
            at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261)
            at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:251)
            at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:336)
            at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2924)
            at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1695)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1627)
        at org.hibernate.loader.Loader.getRow(Loader.java:1509)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:719)
        at org.hibernate.loader.Loader.processResultSet(Loader.java:949)
        at org.hibernate.loader.Loader.doQuery(Loader.java:917)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:348)
        at org.hibernate.loader.Loader.doList(Loader.java:2550)
        at org.hibernate.loader.Loader.doList(Loader.java:2536)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2366)
        at org.hibernate.loader.Loader.list(Loader.java:2361)
        at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1587)
        at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
        at com.shopping.dao.UserDaoImpl.getShopping(UserDaoImpl.java:75)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
    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:745)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
        at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
        at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
        at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)

如果有任何帮助,我将不胜感激

【问题讨论】:

请添加完整的堆栈跟踪 添加了堆栈跟踪 第一部分在玩具之后被截断。最后一部分从'at ...'开始。所以我们仍然缺少堆栈跟踪的关键部分。 更新的堆栈跟踪 【参考方案1】:

原来错误是由 mySQL 表中的错误日期引起的,我没有正确格式化日期。

【讨论】:

以上是关于org.hibernate.exception.GenericJDBCException:无法执行查询] 根本原因的主要内容,如果未能解决你的问题,请参考以下文章