Hibernate:字段列表中的未知表''即使表肯定存在

时间:2017-02-23 10:10:29

标签: java mysql spring hibernate spring-mvc

我是新手,我在休眠方面遇到了问题。当我跑,它抛出异常:

  

HTTP状态500 - 请求处理失败;嵌套异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet

错误:

  

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:未知表   字段列表中的'dotnet0_'

这是我的DB。

    CREATE TABLE `dotnet` (
  `general` double DEFAULT NULL,
  `c#` double DEFAULT NULL,
  `vb` double DEFAULT NULL,
  `asp` double DEFAULT NULL,
  `entityFramework` double DEFAULT NULL,
  `mvc` double DEFAULT NULL,
  `aspNetWebApi` double DEFAULT NULL,
  `ado` double DEFAULT NULL,
  `wpf` double DEFAULT NULL,
  `wcf` double DEFAULT NULL,
  `nunit` double DEFAULT NULL,
  `moq` double DEFAULT NULL,
  `urf` double DEFAULT NULL,
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

例外:

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:977)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:867)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
root cause
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
    org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
    org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
    org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
    org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
    org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
    org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
    org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
    org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
    org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2551)
    org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
    a.b.c.dao.AbstractDAO.getByKey(AbstractDAO.java:29)
    a.b.c.dao.DotNetDAOImpl.findById(DotNetDAOImpl.java:13)
    a.b.c.dao.DotNetDAOImpl.findById(DotNetDAOImpl.java:1)
    a.b.c.service.DotNetServiceImpl.save(DotNetServiceImpl.java:27)
    a.b.c.service.DotNetServiceImpl.save(DotNetServiceImpl.java:1)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    com.sun.proxy.$Proxy228.save(Unknown Source)
    a.b.c.controller.HomeController.changeValue(HomeController.java:186)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:867)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
root cause
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown table 'dotnet0_' in field list
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
    com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
    com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
    com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
    com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
    com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1266)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
    org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
    org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
    org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
    org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
    org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
    org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
    org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
    org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
    org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2551)
    org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
    a.b.c.dao.AbstractDAO.getByKey(AbstractDAO.java:29)
    a.b.c.dao.DotNetDAOImpl.findById(DotNetDAOImpl.java:13)
    a.b.c.dao.DotNetDAOImpl.findById(DotNetDAOImpl.java:1)
    a.b.c.service.DotNetServiceImpl.save(DotNetServiceImpl.java:27)
    a.b.c.service.DotNetServiceImpl.save(DotNetServiceImpl.java:1)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    com.sun.proxy.$Proxy228.save(Unknown Source)
    a.b.c.controller.HomeController.changeValue(HomeController.java:186)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:867)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

我的代码在这里:

    @Entity
@Table(name="dotnet")
public class DotNet {
    private double general;
    private int id;

    // Language
    double cThang;
    double vb;
    double asp;

    // Framework
    private double entityFramework;
    private double mvc;
    private double aspNetWebAPI;
    private double ado;
    private double wpf;
    private double wcf;
    private double nunit;
    private double moq;
    private double urf;

    public double[] toArray() {
        double[] net = { general, cThang, vb, asp, entityFramework, mvc, aspNetWebAPI, ado, wpf, wcf, nunit, moq, urf };
        return net;
    }

    @Column(name = "entityframework", nullable = false)
    public double getEntityFramework() {
        return entityFramework;
    }

    public void setEntityFramework(double entityFramework) {
        this.entityFramework = entityFramework;
    }

    @Column(name = "mvc", nullable = false)
    public double getMvc() {
        return mvc;
    }

    public void setMvc(double mvc) {
        this.mvc = mvc;
    }

    @Column(name = "aspnetwebapi", nullable = false)
    public double getAspNetWebAPI() {
        return aspNetWebAPI;
    }

    public void setAspNetWebAPI(double aspNetWebAPI) {
        this.aspNetWebAPI = aspNetWebAPI;
    }

    @Column(name = "ado", nullable = false)
    public double getAdo() {
        return ado;
    }

    public void setAdo(double ado) {
        this.ado = ado;
    }

    @Column(name = "wpf", nullable = false)
    public double getWpf() {
        return wpf;
    }

    public void setWpf(double wpf) {
        this.wpf = wpf;
    }

    @Column(name = "nunit", nullable = false)
    public double getNunit() {
        return nunit;
    }

    public void setNunit(double nunit) {
        this.nunit = nunit;
    }

    @Column(name = "moq", nullable = false)
    public double getMoq() {
        return moq;
    }

    public void setMoq(double moq) {
        this.moq = moq;
    }

    @Column(name = "URF", nullable = false)
    public double getUrf() {
        return urf;
    }

    public void setUrf(double urf) {
        this.urf = urf;
    }

    @Column(name = "c#", nullable = false)
    public double getcThang() {
        return cThang;
    }

    public void setcThang(double cThang) {
        this.cThang = cThang;
    }

    @Column(name = "vb", nullable = false)
    public double getVb() {
        return vb;
    }

    public void setVb(double vb) {
        this.vb = vb;
    }

    @Column(name = "asp", nullable = false)
    public double getAsp() {
        return asp;
    }

    public void setAsp(double asp) {
        this.asp = asp;
    }

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "general", nullable = false)
    public double getGeneral() {
        return general;
    }

    public void setGeneral(double general) {
        this.general = general;
    }

    @Column(name = "wcf", nullable = false)
    public double getWcf() {
        return wcf;
    }

    public void setWcf(double wcf) {
        this.wcf = wcf;
    }
}

我的配置:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "a.b.c")
public class AppConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }

}

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet(
                "dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }

}

这里是hibernate config:

@Configuration
@EnableTransactionManagement
@ComponentScan({ "a.b.c.configuration" })
@PropertySource(value = { "classpath:application.properties" })

public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "a.b.c.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));

        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;        
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }

}

和属性:

#DB properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ntq?autoReconnect=true
jdbc.username=account
jdbc.password=password

#Hibernate Configuration:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
hibernate.format_sql=true
#entitymanager.packages.to.scan=com.javacodegeeks

如果这是一个重复的问题,我很抱歉。一旦我知道导致错误的原因,我会将其标记为重复。谢谢!

0 个答案:

没有答案