Spring Hibernate MySQL,MySQLSyntaxErrorException:table不存在

时间:2015-01-29 18:53:47

标签: mysql spring hibernate integration

在这里,我发布了过去一周我一直在处理的例外情况。

                     HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement


            type Exception report

            message Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement

            description The server encountered an internal error that prevented it from fulfilling this request.

            exception 
            org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
                org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
                org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
                org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:722)



            root cause 
            org.hibernate.exception.SQLGrammarException: could not execute statement
                org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
                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.executeUpdate(ResultSetReturnImpl.java:190)
                org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
                org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
                org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
                org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
                org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
                org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
                org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
                org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
                org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
                org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
                org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
                org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
                org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
                org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
                org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
                org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
                org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
                org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
                org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
                org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
                org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
                com.project.pms.service.LoginService.createUser(LoginService.java:65)
                com.project.pms.controller.LoginController.createUser(LoginController.java:56)
                sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                java.lang.reflect.Method.invoke(Method.java:597)
                org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
                org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
                org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
                org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
                org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
                org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
                org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
                org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
                org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
                org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
                org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:722)



            root cause 
            com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'pms.itemmodel' doesn't exist
                sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
                sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
                java.lang.reflect.Constructor.newInstance(Constructor.java:513)
                com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
                com.mysql.jdbc.Util.getInstance(Util.java:360)
                com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
                com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
                com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
                com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
                com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
                com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530)
                com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907)
                com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141)
                com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077)
                com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062)
                org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
                org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
                org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
                org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
                org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
                org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
                org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
                org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
                org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
                org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
                org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
                org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
                org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
                org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
                org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
                org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
                org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
                org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
                org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
                org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
                org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
                org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
                com.project.pms.service.LoginService.createUser(LoginService.java:65)
                com.project.pms.controller.LoginController.createUser(LoginController.java:56)
                sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                java.lang.reflect.Method.invoke(Method.java:597)
                org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
                org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
                org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
                org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
                org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
                org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
                org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
                org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
                org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
                org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
                org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
                javax.servlet.http.HttpServlet.service(HttpServlet.java:722)



            note The full stack trace of the root cause is available in the Apache Tomcat/7.0.32 logs.

pom.xml(只是为了让你了解我正在使用的版本)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.project.pms</groupId>
      <artifactId>pms</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>pms Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        <!--SPRING FRAMEWORK DEPENDANCIES 
         -->
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.5.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>


    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>



      </dependencies>
      <build>
        <finalName>pms</finalName>
      </build>
    </project>

的web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app>
      <display-name>PMS</display-name>
      <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>

      <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    </web-app>

Dispatcher Servlet

    <?xml version="1.0" encoding="UTF-8"?>
    <beans  xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.2.xsd">

            <mvc:annotation-driven></mvc:annotation-driven>
            <context:component-scan base-package="com.project.pms"></context:component-scan>

            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
            </bean>

            <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/pms"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
            </bean>

            <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource"></property>
            <property name="packagesToScan" value="com.project.pms"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
            </bean> 


            <import resource="codeSpecificBeans.xml"/>


    </beans>

调度程序servlet中导入的资源(codeSpecificBeans.xml)

    <beans  xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.2.xsd">



    <bean id="loginService"  class="com.project.pms.service.LoginService">
    </bean>


    <bean id="itemService"  class="com.project.pms.service.ItemService">
    </bean>




    </beans>

ItemModel

    package com.project.pms.model;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;


    @Entity
    public class ItemModel
    {


        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private String itemId;
        private String itemName;
        private String itemType;
        private String itemSubType;
        private String itemDescription;
        private String itemImageUrl;
        private String itemPrice;
        private String itemVolumePerUnit;
        private String itemCompanyName;



        private String currentItemQtyInStock;
        private String itemUnitsOfMeasurement;// ex % , KG ,
                    //..... and so on with getters and setters 
        }

登录控制器(请专注于方法createUser();)

            package com.project.pms.controller;

            import javax.servlet.http.HttpServletRequest;

            import org.hibernate.SessionFactory;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.stereotype.Controller;
            import org.springframework.ui.ModelMap;
            import org.springframework.web.bind.annotation.ModelAttribute;
            import org.springframework.web.bind.annotation.RequestMapping;

            import com.project.pms.beans.ItemBean;
            import com.project.pms.beans.UserBean;
            import com.project.pms.service.LoginService;
            import com.project.pms.utils.DatabaseQueries;



            @Controller
            public class LoginController
            {


                @Autowired
                LoginService loginService;

                @Autowired
                SessionFactory sessionFactory;


                @ModelAttribute("userbean")
                public UserBean getUserBean(){
                    return new UserBean();
                }
                @ModelAttribute("itemBean")
                public ItemBean getItemBean(){
                    return new ItemBean();
                }

                @RequestMapping(value="/login")
                public String loadLogin()
                {
            //      DatabaseQueries.populateDataBaseAtStartup(sessionFactory);
                    return "login/Login";
                }

                @RequestMapping(value="/signup")
                public String loadSignUp()
                {
                    return "login/SignUp";
                }

                @RequestMapping(value="/createUser")
                public String createUser(@ModelAttribute("userbean") UserBean userBean, HttpServletRequest request)
                {
                    boolean createUserSuccessful=loginService.createUser(userBean);
                    if(createUserSuccessful)
                    {return "login/Login";
                    }
                    else 
                    {
                        request.setAttribute("createUserSuccessful", createUserSuccessful);
                        return "login/SignUp";
                    }
                }

                @RequestMapping(value="/authenticate")
                public String authenticateLogin(@ModelAttribute("userbean") UserBean userBean, HttpServletRequest request,ModelMap model)
                {
                    System.out.println("Authenticating");
                    boolean authenticateLogin=loginService.retrieveAuthenticationDetails(userBean);
                    if(authenticateLogin)
                    {
                    request.getSession().setAttribute("username", userBean.getUsername());  
                    model.addAttribute("userbean",userBean);

                    return "items/AllItems";
                    }
                    else
                    request.setAttribute("authenticationFailed", true); 
                    return "login/Login";   

                }


            }

LoginService - 仅限createUser()方法

  public boolean createUser(UserBean userBean)
                {

                userModel=UserHelper.convertBeanToModel(userBean);  
                ItemModel itemModel= new ItemModel();
                itemModel.setItemName("Q");
                itemModel.setItemType("Q");
                itemModel.setItemSubType("Q");
                itemModel.setItemDescription("Q");
                itemModel.setItemCompanyName("Q");
                try{

                Session session=getSession();
                session.beginTransaction();

                session.save(itemModel);
                session.save(userModel);

                session.getTransaction().commit();
                session.close();
                }
                catch(ConstraintViolationException e)
                {

                    return false;
                }

                    return true;
                }

除此之外,你可能想知道的一些事情是:
1)我使用的是MySQL 5.2.37版本。

2)如果我只坚持userModel,应用程序运行绝对正常,对于ItemModel howerver似乎有一些我无法理解的问题()

3)b / w userModel和itemModel没有太大的区别,只是在itemModel中有一个生成策略

我试过的策略包括将hbm2ddl.auto更改为&#34; create-drop&#34; ,将Dialect改为MySQL5InnoDb,我希望这不是MYSQL的缺陷,而且是我的一些错误。请帮我解决这个问题,(如果这个问题没有得到解决,我可能会死,严重的事情。)

1 个答案:

答案 0 :(得分:0)

 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'pms.itemmodel' doesn't exist

例外情况表明名称没有表格&#34; pms.itemmodel&#34;。

我注意到的一些事情如下:

以下是您的班级声明。您已在包&#34; com.project.pms.model&#34;中定义了ItemModel。

package com.project.pms.model;

@Entity
public class ItemModel
{

但是在packagesToScan中你定义为

<property name="packagesToScan" value="com.project.pms"></property>

&#34; packagesToScan&#34;不会查看子包(com.project.pms.model)内部,只会查看指定的包(&#34; com.project.pms&#34;)。

http://forum.spring.io/forum/spring-projects/data/68231-annotationsessionfactorybean-packagestoscan-scanning-of-sub-packages

此外,您的实体类没有@Table(name =&#34; some-name&#34;)注释。这意味着您的不合格类名称&#39; ItemModel&#39;并且表名是相同的。如果它们不包含@Table注释并指定正确的表名。