有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java获取SQLSyntaxErrorException:hibernate中不存在表

我正在尝试编写一个非常简单的hibernate应用程序。但是当我运行main方法时,它会抛出异常SQLSyntaxErrorException: Table 'testhibernate.employee' doesn't exist,即使我已经将hibernate.hbm2ddl.auto设置为“create”。 详情如下。你能帮我一下吗

使用main方法初始化:

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: Table 'testhibernate.employee' doesn't exist at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) at com.mysql.cj.jdbc.exceptions.SQLError.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) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040) at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ... 20 more

MySql版本:5.5.50

休眠版本:5.4.25。决赛


共 (1) 个答案

  1. # 1 楼答案

    根据documentation的说法:

    The entity class must have a public or protected no-argument constructor. It may define additional constructors as well.

    因此,你应该用以下方式纠正你的实体:

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