首页 > 解决方案 > 获取 SQLSyntaxErrorException:表在休眠中不存在

问题描述

我正在尝试编写一个非常简单的休眠应用程序。但是当我运行主方法时,SQLSyntaxErrorException: Table 'testhibernate.employee' doesn't exist即使我设置hibernate.hbm2ddl.auto为“创建”,它也会抛出异常。详情如下。你能帮我解决这个问题吗?

具有主要方法的类:

package com.sr.main;

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

import com.sr.entity.Employee;

public class TestHibernate {

    public static void main(String[] args) {
        // TODO Auto-generated method stubu
        Configuration config = new Configuration().configure("\\hibernate.cfg.xml");
        SessionFactory factory  = config.buildSessionFactory();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        Employee emp1 = new Employee(111, "John",20000);
        session.save(emp1);
        tx.commit();
        

    }

}

实体类:

package com.sr.entity;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee {
    
    @Id
    Integer empId;
    String empName;
    Integer empSal;
    
    
    public Employee(Integer empId, String empName, Integer empSal) {
        super();
        this.empId = empId;
        this.empName = empName;
        this.empSal = empSal;
    }
    public Integer getEmpId() {
        return empId;
    }
    public void setEmpId(Integer empId) {
        this.empId = empId;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public Integer getEmpSal() {
        return empSal;
    }
    public void setEmpSal(Integer empSal) {
        this.empSal = empSal;
    }
    

}

配置xml:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name = "hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name = "hibernate.connection.url">
         jdbc:mysql://localhost/testHibernate
      </property>
      <property name = "hibernate.connection.username">
         root
      </property>
      <property name = "hibernate.connection.password">
         root
      </property>
      <property name="hibernate.hbm2ddl.auto">create</property>
      <property name="show_sql">true</property>
      
      <!-- List of XML mapping files -->
      <mapping class = "com.sr.entity.Employee"/>
      
   </session-factory>
</hibernate-configuration>

例外 :

java.sql.SQLSyntaxErrorException:com.mysql.cj.jdbc.exceptions.SQLError 的 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) 中不存在表“testhibernate.employee”。 createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) at 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) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) 在 org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ... 20 更多

MySql 版本: 5.5.50

休眠版本: 5.4.25.Final

标签: javamysqlhibernate

解决方案


根据文档

实体类必须具有公共或受保护的无参数构造函数。它也可以定义额外的构造函数。

因此,您应该通过以下方式更正您的实体:

@Entity
public class Employee {
    
   // ...    
   public Employee() {
   }

   public Employee(Integer empId, String empName, Integer empSal) {
       this.empId = empId;
       this.empName = empName;
       this.empSal = empSal;
   }
   
   // ...
}

推荐阅读