java.sql.SQL语法错误异常 : Table 'mydb.alien' doesn't exist

标签 java mysql hibernate maven

Alien.java(Pojo 类), 这是我的 pojo 类

  package com.me.Hive1;
  import javax.persistence.Entity;
  import javax.persistence.Id;

  @Entity
  public class Alien {          
        @Id
        private int id;
        private String name;
        public int getId()
        {
            return id;
        }
        public void setId(int id)
        {
            this.id = id;
        }
        public String getName()
        {
             return name;
        }
        public void setName(String name)
        {
             this.name = name;
        }
     }

App.java(主类), 这是主类。

package com.me.Hive1;
            import org.hibernate.Session;
            import org.hibernate.SessionFactory;
            import org.hibernate.Transaction;
            import org.hibernate.cfg.Configuration;
            //MAIN CLASS
            /**
             * Hello world!
             *
             */
            public class App 
            {
                public static void main( String[] args )
                {
                    System.out.println( "Hello World!" );
                    Alien a = new Alien();
                    a.setId(101);
                    a.setName("bhanu");
                    try{
                    Configuration fact = new Configuration().configure().addAnnotatedClass(Alien.class);
                    SessionFactory sf = fact.buildSessionFactory();
                    Session session = sf.openSession();
                    Transaction t = session.beginTransaction();
                    session.save(a);
                    t.commit();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }

                }
            }

配置文件(hibernate.cfg.xml), 这是我的 hibernate 配置文件。

<?xml version="1.0" encoding="UTF-8"?><!--Configuration file-->
        <!DOCTYPE hibernate-configuration PUBLIC
                "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
        <hibernate-configuration>
            <session-factory>
                <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="hibernate.connection.password">root</property>
                <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
                <property name="hibernate.connection.username">root</property>
                <property name="hibernate.hbm2dll.auto">create</property>
                <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
                <mapping class="com.me.Hive1.Alien" resource="Alien.hbm.xml"/>
            </session-factory>
        </hibernate-configuration>

Alien.hbm.xml(映射文件), 这是映射文件,它是可选的吗?

<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated Jul 5, 2017 1:44:20 AM by Hibernate Tools 3.5.0.Final -->
    <hibernate-mapping>
        <class name="com.me.Hive1.Alien" table="alien">
            <id name="id" type="int">
                <column name="ID" />
                <generator class="assigned" />
            </id>
            <property name="name" type="java.lang.String">
                <column name="NAME" />
            </property>
        </class>
    </hibernate-mapping>

我得到异常(控制台输出)这是我得到的异常, 不知道我被击中了

    Hello World!
    Jul 05, 2017 4:13:18 PM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {5.2.10.Final}
    Jul 05, 2017 4:13:18 PM org.hibernate.cfg.Environment <clinit>
    INFO: HHH000206: hibernate.properties not found
    Jul 05, 2017 4:13:19 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
    INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
    Jul 05, 2017 4:13:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
    Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
    Jul 05, 2017 4:13:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/mydb]
    Jul 05, 2017 4:13:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001001: Connection properties: {user=root, password=****}
    Jul 05, 2017 4:13:19 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
    INFO: HHH10001003: Autocommit mode: false
    Jul 05, 2017 4:13:19 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
    INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
    Wed Jul 05 16:13:19 IST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    Jul 05, 2017 4:13:19 PM org.hibernate.dialect.Dialect <init>
    INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
    Jul 05, 2017 4:13:24 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    WARN: SQL Error: 1146, SQLState: 42S02
    Jul 05, 2017 4:13:24 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    ERROR: Table 'mydb.alien' doesn't exist
    Jul 05, 2017 4:13:24 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
    INFO: HHH000010: On release of batch it still contained JDBC statements
    Jul 05, 2017 4:13:24 PM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
    ERROR: HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
        at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1441)
        at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:491)
        at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3201)
        at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2411)
        at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
        at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
        at com.me.Hive1.App.main(App.java:25)
    Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
        at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3003)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3503)
        at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
        at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589)
        at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
        at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
        at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
        at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1435)
        ... 9 more
    Caused by: java.sql.SQLSyntaxErrorException: Table 'mydb.alien' doesn't exist
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:536)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
        at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)
        at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1826)
        at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2034)
        at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:1970)
        at com.mysql.cj.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5001)
        at com.mysql.cj.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1955)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:205)
        ... 18 more

最佳答案

试试这个 -

在您的配置文件中更改此属性的值:

<property name="hibernate.hbm2ddl.auto" value="update" />

希望这对你有帮助;)

关于java.sql.SQL语法错误异常 : Table 'mydb.alien' doesn't exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44924606/

相关文章:

java - JDBC批处理操作理解

java - 如果所有元素 xpath 都相同,如何区分元素

Java Hibernate 从某个索引或偏移量获取列表。

java - Hibernate/mysql 连接问题

php - 将带有数字的字符串传递到 mysql 其中

mysql - 我可以允许用户仅使用他的数据更改表吗?

php - substring_index 存储到 session

Java Hibernate 与数据库的连接几分钟后超时

java - 数组对象的数组

java - REST最佳实践,用于检索元素的特定子集