在Hibernate中使用 @JoinColumn时出现错误
在使用Hibernate时,我遇到了这个错误
@JoinColumn
org.hibernate.exception.SQLGrammarException: 无法执行语句 [Unknown column 'department_id' in 'field list'] [insert into employees (department_id,hire_date,name,position,salary) values (?,?,?,?,?)]
我在使用关联列,以为在连接表时,department_id这个别名会被生成并作为外键。
我有两个实体和两张表。第一张表名为employees
CREATE TABLE employees (
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(50),
position VARCHAR(50),
salary DECIMAL(10, 2),
hire_date DATE
);
以及对应的实体
package codefinity.model;
import jakarta.persistence.*;
import lombok.*;
import java.util.Date;
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name")
private String name;
@Column(name = "position")
private String position;
@Column(name = "salary")
private double salary;
@Column(name = "hire_date")
private Date hireDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "department_id")
private Department department;
public Employee() {
}
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;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
第二张表
CREATE TABLE departments (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
location VARCHAR(100)
);
以及对应的实体
package codefinity.model;
import jakarta.persistence.*;
@Entity
@Table(name ="departments")
public class Department {
public Department() {
}
public Department(String name, int id, String location) {
this.name = name;
this.id = id;
this.location = location;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name ="name")
private String name;
@Column(name = "location")
private String location;
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;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
Hibernate配置文件
<?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>
<!-- JDBC set up-->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testDatabase</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1234</property>
<!-- Dialect setup -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- SQL Display and Formatting Settings -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
下面是失败的测试方法
@Test
public void testEmployeeEntityMapping() {
Department department = new Department();
department.setName("Development");
department.setLocation("Main Office");
session.persist(department);
Employee employee = new Employee();
employee.setName("John Doe");
employee.setPosition("Developer");
employee.setSalary(75000);
employee.setHireDate(new Date());
employee.setDepartment(department);
session.persist(employee);
session.flush();
session.clear();
Employee retrievedEmployee = session.get(Employee.class, employee.getId());
assertNotNull(retrievedEmployee, "Employee was not retrieved, mapping may be incorrect.");
assertNotNull(retrievedEmployee.getDepartment(), "Department of the retrieved employee is null, association mapping may be incorrect.");
}
是配置有问题吗,还是Join没有为department_id创建别名?
我的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>codefinity</groupId>
<artifactId>Hibernate_Entity_Creation_Task</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.3.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.46</version>
<scope>compile</scope>
</dependency>
<!-- JUnit 5 Jupiter API -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<!-- JUnit 5 Jupiter Engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
</project>
解决方案
org.hibernate.exception.SQLGrammarException: 无法执行语句 [Unknown column 'department_id' in 'field list'] [insert into employees (department_id,hire_date,name,position,salary) values (?,?,?,?,?)]
此错误表示你向未知字段插入了:department_id。因为 employees.id 是 employees 表的主键。你需要在 employees 表中新增一个名为 department_id 的列,作为对 departments.id 的外键引用。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。