我创建了Employee和Department实体。它们具有双向多对一和一对多的关系。
使用REST客户端,我添加了部门和员工。
添加部门:
{
"name":"IT"
}
添加员工:
{"name": "Emp01" ,
"address":
{
"street" :"Street01" ,
"city" :"City01" ,
"state" :"State01" ,
"country" :"Country01"
},
"department":{"id":"1"}
}
现在,我想通过关联现有员工来添加新部门。
{
"name":"Admin",
"employees":
[{
"id":3
}]
}
现在,部门已成功添加,但员工表未正确更新。除了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();
}
}
答案 0 :(得分:1)
您正在使用javax.persistence
包进行实施,但您将问题标记为Hibernate
。 Hibernate
只是JPA(Java Persistence API)的一个实现。甚至Hibernate
网页也建议使用JPA。除非您维护遗留代码,否则最好使用JPA API来保存和获取实体。
现在回答你的问题:
现在,我想通过关联现有员工来添加新部门。
为了能够持久化以及更新您的实体,您应该遵循以下步骤(代码行只是pseodo代码):
创建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);
您应该在事务中执行所有这些操作,并且一切都应该按预期工作。