java - 将数据添加到相关表 Spring Hibernate
问题描述
我有两个实体:
Employee:
@Entity
@Table(name="employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "employeeId")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "employeeId")
private EmployeeDetail employeeDetail;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public EmployeeDetail getEmployeeDetail() {
return employeeDetail;
}
public void setEmployeeDetail(EmployeeDetail employeeDetail) {
this.employeeDetail = employeeDetail;
}
}
EmployeeDetail:
@Entity
@Table(name = "employee_detail")
public class EmployeeDetail {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="employee_detail_id")
private int id;
@Column(name="work_experience")
private int workExperience;
@Column(name="hobby")
private String hobby;
@Column(name="nationality")
private String nationality;
@OneToOne(mappedBy = "employeeDetail")
private Employee employee;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getWorkExperience() {
return workExperience;
}
public void setWorkExperience(int workExperience) {
this.workExperience = workExperience;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
通过这种方式,我将数据存储到Employee
表中:
EmployeeController:
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/list")
public String listEmployee(Model theModel) {
// get customers from the service
List<Employee> theEmployee = employeeService.listEmployee();
// add the customers to the model
theModel.addAttribute("employees", theEmployee);
return "list-employee";
}
@GetMapping("/showFormForAddNewEmployee")
public String showFormForAddNewEmployee(Model theModel) {
// create model attribute to bind form data
Employee theEmployee = new Employee();
theModel.addAttribute("employee", theEmployee);
return "addNewEmployeeForm";
}
@PostMapping("/saveEmployee")
public String saveEmployee(@ModelAttribute("customer") Employee theEmployee) {
// save the customer using our service
employeeService.saveEmployee(theEmployee);
return "redirect:/employee/list";
}
}
如何为相关表创建类似的东西?我很困惑
解决方案
推荐阅读
- python - SyntaxError:(unicode 错误)“unicodeescape”编解码器无法使用 Selenium Python 解码位置字节
- python - 如何查看约束 IIS gurobi
- javascript - 为什么我会收到“ReferenceError:未定义测试”
- javascript - 验证函数的返回应该如何中断或继续另一个函数?
- sql - 检查字符串的前半部分是否为字符
- javascript - jQuery focusin / focusout - 以编程方式更新页面时触发 - 如何检测 *only* 人类关注的时间?
- wordpress - 适用于 SFTP 的 Amazon AWS EC2 Openlitespeed 权限
- math - 我们如何正确判断两个浮点变量是否相等?
- azure-devops - Azure DevOps:管道构建中的 Blazor 部署失败
- next.js - 使用 Next.js 进行机车滚动