java.sql.SQLSyntaxErrorException:表'mydb.alien'不存在

Posted

技术标签:

【中文标题】java.sql.SQLSyntaxErrorException:表\'mydb.alien\'不存在【英文标题】:java.sql.SQLSyntaxErrorException: Table 'mydb.alien' doesn't existjava.sql.SQLSyntaxErrorException:表'mydb.alien'不存在 【发布时间】:2017-12-09 00:38:18 【问题描述】:

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), 这是我的休眠配置文件。

<?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

【问题讨论】:

即使我没有使用 maven 我也遇到了同样的异常,我一直在尝试从一周没有解决方案到现在请有人帮我解决这个问题。这是什么原因?????????? 试试这个:@Column 私有字符串名称;而不是私有字符串名称;在实体类中 您能分享您的确切表名或创建表 SQL 吗? @Nidhi257, `1)org.hibernate.service.spi.ServiceException: 无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 2) 原因:org .hibernate.exception.SQLGrammarException:调用Driver#connect 3)错误:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知数据库'mydb'。为您的更改引发此异常!!!! @DarshitChokshi,桌子?????? hibernate应该创建表吧???我没有创建任何表,但它说 mydb.alien 即使我不知道! 【参考方案1】:

试试这个 -

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

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

希望这会对你有所帮助;)

【讨论】:

终于,我得到了一些燃料来运行休眠,非常感谢 同样的问题,这没有帮助。

以上是关于java.sql.SQLSyntaxErrorException:表'mydb.alien'不存在的主要内容,如果未能解决你的问题,请参考以下文章