首页 > 解决方案 > 将数据添加到相关表 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";
    }
}

如何为相关表创建类似的东西?我很困惑

标签: javaspringhibernate

解决方案


推荐阅读