gpt4 book ai didi

java - 为什么关联表无法使用 hibernate 正确更新?

转载 作者:行者123 更新时间:2023-12-02 11:46:33 26 4
gpt4 key购买 nike

我已经创建了员工和部门实体。这些具有双向多对一和一对多关系。

使用 REST 客户端,我添加部门和员工。

添加部门:

{
"name":"IT"
}

enter image description here

添加员工:

{"name": "Emp01" ,
"address":
{
"street" :"Street01" ,
"city" :"City01" ,
"state" :"State01" ,
"country" :"Country01"
},
"department":{"id":"1"}
}

enter image description here

现在,我想通过关联现有员工来添加新部门。

{
"name":"Admin",
"employees":
[{
"id":3
}]
}

enter image description here

enter image description here

现在,部门已成功添加,但员工表未正确更新。除 id 外,其他字段均被空白。理想情况下,hibernate 应该使用最新的部门 ID 更新员工。请让我知道这里可能缺少什么?谢谢。

Employee.java

package com.empcatalogue.model;

import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

private String name;

@Embedded
private Address address;

@ManyToOne(cascade = CascadeType.MERGE)
private Department department;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public Department getDepartment() {
return department;
}

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

Department.java

package com.empcatalogue.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Department {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

private String name;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "department", fetch = FetchType.EAGER)
private List<Employee> employees = new ArrayList<>();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public List<Employee> getEmployees() {
return employees;
}

public void setEmployees(List<Employee> employees) {
this.employees = employees;
}

}

EmployeeRepository.java

...
...
...
public void addEmployee(Employee emp) {


Session session = RepoManager.getSessionFactory().openSession();
try {
session.beginTransaction();

session.save(emp);

session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
} finally {
session.close();
}
}

DepartmentRepository.java

...
...
...
public void addDepartment(Department department) {
Session session = RepoManager.getSessionFactory().openSession();
try {
session.beginTransaction();

session.save(department);

session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
} finally {
session.close();
}

}

最佳答案

您正在使用 javax.persistence 包进行实现,但您将问题标记为 HibernateHibernate只是JPA(Java Persistence API)的实现之一。甚至 Hibernate 网页也推荐使用 JPA。除非您维护遗留代码,否则最好使用 JPA API 来持久保存和获取您的实体。

现在回答你的问题:

Now, I want to add a new department by associating an existing employee.

为了能够保留并更新您的实体,您应该遵循以下步骤(代码行只是伪代码):

  • 创建 Department 实体的实例:

    Department dept = new Department();
    dept.setName(...);
  • 在事务中从数据库中获取现有的 Employee 实体:

    Employee emp = session.get(Employee.class, <employee_id_here>);
  • 将实体实例连接在一起:

    dept.getEmployees().add(emp);
    emp.setDepartment(dept);
  • 坚持员工:

    session.save(dept);

您应该在事务中完成所有这些操作,并且一切都应该按预期进行。

关于java - 为什么关联表无法使用 hibernate 正确更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48160846/

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