gpt4 book ai didi

java - hibernate 时无法保存数据

转载 作者:行者123 更新时间:2023-12-02 12:06:52 24 4
gpt4 key购买 nike

我正在尝试将数据保存到 hibernate 状态,但由于错误而无法保存:

12:39:24,006 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-4) Unknown column 'department_id' in 'field list'

请找到下面的问题陈述:

这是我的实体类:

@Entity
@Table(name = "employee")
@Where(clause = "isDeleted = false")
public class Employee extends BaseEntity {

@OneToOne
private Department department;

private int status;

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}


}

员工表:

id |部门 |状态

我正在插入这样的数据:

 @Inject
CrudService<Employee> employeeManager;

Department department = new Department();

// create department and after creating depaerment

department.setDepartment(department);
department.setStatus(1);
department.setId(null);

employeeManager.create(department);

我在保存数据时收到错误:

insert into employee (isdeleted, last_modification, department_id, status) values (?, ?, ?, ?)

here i can see that my Employee Table conatins department column but hibernate is generating department_id

在内部插入记录。

有人可以帮我吗?如果您需要更多信息,请返回。

最佳答案

首先,我猜很多员工属于同一个部门,所以应该是ManyToOne。其次,您可以使用 JoinColumn 指定...连接列的名称:

@ManyToOne
@JoinColumn(name = "department")
private Department department;

但我宁愿更改数据库中列的名称,因为 department_id 可以更清楚地表明该列包含部门的 ID,因此是外键。

关于java - hibernate 时无法保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46844358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com