java - 获取 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
解决方案
根据文档:
实体类必须具有公共或受保护的无参数构造函数。它也可以定义额外的构造函数。
因此,您应该通过以下方式更正您的实体:
@Entity
public class Employee {
// ...
public Employee() {
}
public Employee(Integer empId, String empName, Integer empSal) {
this.empId = empId;
this.empName = empName;
this.empSal = empSal;
}
// ...
}
推荐阅读
- c# - 我怎样才能持有一个弱引用
到实例的方法,直到实例被收集? - swift - 设置scnnode展示位置
- php - htaccess 文件将 /stringa 重定向到 /index.php?action=stringb
- javascript - 如何使用 id 获取多个复选框值
- python - 如何在张量流中实现随机池化,在每个滑动窗口中获取随机像素
- python-3.x - 获取列表中的一组数字,其总和与给定的所需数字匹配
- python - 插值标准函数 python, func(10,16,2) -> [10 12 14 16]
- c# - 如何将变量放入c#属性中?
- javascript - 在 x 个字符后阅读更多内容 JavaScript
- notifications - 是否有任何应用程序可以从 Google 日历事件中创建(假)传入 SMS?