【问题标题】:Caused by: java.sql.SQLSyntaxErrorException: Table 'project.playing' doesn't exist [duplicate]引起:java.sql.SQLSyntaxErrorException:表'project.playing'不存在[重复]
【发布时间】:2020-04-24 20:43:18
【问题描述】:

错误:引起:java.sql.SQLSyntaxErrorException:表 'project.playing' 不存在于 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) 在 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) 在 com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347)

在 Eclipse 中使用 maven 项目尝试使用 hibernate 在现有的 mysql 数据库(项目)中创建一个表(正在播放)。 实体类代码:

package com.rj.hibtry.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "playing")
public class Users {

    @Id
    @Column(name = "user_id")
    int userId;

    @Column(name = "username")
    String username;

    @Column(name = "password")
    String password;

    @Column(name = "first_name")
    String firstName;

    @Column(name = "last_name")
    String lastName;


    public Users(String username, String password, String firstName, String lastName) {
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

主要方法代码:

package com.rj.hibtry;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.rj.hibtry.entity.Users;

public class App {

    public static void main(String[] args) {
          SessionFactory factory=new Configuration()
                  .configure("hibernate.cfg.xml")
                  .addAnnotatedClass(Users.class)
                  .buildSessionFactory();
          Session session=factory.getCurrentSession();
          try {
              // Create object of entity class type
              Users user = new Users("lj", "password", "firstName", "lastName");
              // Start transaction
              session.beginTransaction();
              // Perform operation
              session.save(user);
              // Commit the transaction 
              session.getTransaction().commit();
              System.out.println("Row added!");


        } finally {
            session.close();
            factory.close();
        }


      }
}

hibernate.cfg.xml 代码:

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

        <!-- Connection settings -->

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
              <!-- Sample MySQL URL provided  -->  
        <property name="connection.url">jdbc:mysql://localhost:3306/project?serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="hbm2ddl.auto">create</property>

        <!-- Show SQL on console -->
        <property name="show_sql">true</property>



        <!--Setting Session context model -->
        <property name="current_session_context_class">thread</property>

    </session-factory>
</hibernate-configuration>

【问题讨论】:

  • 日志是否显示它正在尝试运行的任何 SQL?
  • Apr Hibernate:删除表(如果存在)正在播放 2020 年 4 月 25 日下午 12:18:12 [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@78a515e4] 用于(非 JTA ) DDL 执行未处于自动提交模式;连接“本地事务”将被提交,连接将设置为自动提交模式。 Hibernate: 创建表播放(user_id integer not null, first_name varchar(255), last_name varchar(255), password varchar(255), username varchar(255), primary key (user_id)) type=MyISAM
  • 休眠:插入播放(first_name、last_name、password、username、user_id)值(?、?、?、?、?)2020 年 4 月 25 日下午 12:18:12 org.hibernate。 engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 1146, SQLState: 42S02 Apr 25, 2020 12:18:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Table 'project.playing' does不存在

标签: java mysql hibernate


【解决方案1】:

使用 MySQL 4.0 以上版本的人需要在配置文件中更改方言。 "org.hibernate.dialect.MySQLDialect" 将工作到 4.0 版本,在此之前您需要使用 "org.hibernate.dialect.MySQL5Dialect"

【讨论】:

    猜你喜欢
    • 2017-12-09
    • 1970-01-01
    • 2016-05-19
    • 2015-05-24
    • 2020-01-05
    • 2023-04-04
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    相关资源
    最近更新 更多