Я получаю эту ошибку в Hibernate при использовании
@JoinColumn
org.hibernate.exception.SQLGrammarException: could not execute statement \[Unknown column 'department_id' in 'field list'\] \[insert into employees (department_id,hire_date,name,position,salary) values (?,?,?,?,?)\]
Я использую столбец соединения и думаю, что псевдоним Department_id создается при объединении таблиц в качестве внешнего ключа.
У меня есть две сущности и две таблицы. Первая таблица — сотрудники
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;
}
}
Файл конфигурации спящего режима
com.mysql.cj.jdbc.Driver
jdbc:mysql://localhost:3306/testDatabase
root
1234
org.hibernate.dialect.MySQLDialect
true
true
thread
А вот метод тестирования, который не работает
@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 не создает псевдоним для идентификатора отдела.
-ИЗМЕНИТЬ Мой файл pom.xml
4.0.0
codefinity
Hibernate_Entity_Creation_Task
1.0-SNAPSHOT
21
21
UTF-8
org.hibernate.orm
hibernate-core
6.3.1.Final
org.projectlombok
lombok
1.18.46
compile
org.junit.jupiter
junit-jupiter-api
5.8.2
test
org.junit.jupiter
junit-jupiter-engine
5.8.2
test
mysql
mysql-connector-java
8.0.33