本文介绍了使用Hibernate Annotation从父表创建并填充子表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要从表EMPLOYEE创建一个表EMPLOYEE_REMARK。 并且需要使用Annotation Hibernate来完成。 EMPLOYEE EMP_ID,EMP_FNAME,EMP_LNAME EMPLOYEE_REMARK EMP_REMARK_ID,EMP_ID,备注 它将成为OnetoOne关系,即对于每个EMP_ID都会有一个备注。 REMARK可能为null。 请帮我解决问题... 可以通过从员工创建一个类并从中填充EMPLOYEE_REMARK来完成吗? 解决方案基本上这就是做你想做的事的方式。 员工 @Entity @Table(name =EMPLOYEE) public class Employee实现Serializable { @Id @Column(name =EMP_ID) private Long id; @Column(name =EMP_FNAME) private String firstName; @Column(name =EMP_LNAME) private String lastName; @OneToOne(mappedBy =employee,cascade = CascadeType.ALL, orphanRemoval = true)私人EmployeeRemark employeeRemark; public void setRemark(String remark){ this.employeeRemark = new EmployeeRemark(); this.employeeRemark.setRemark(remark); this.employeeRemark.setEmployee(this); } public String getRemark(){ return employeeRemark == null? null:employeeRemark.getRemark(); } // getters and setters } 员工备注 @Entity @Table(name =EMPLOYEE_REMARK) public class EmployeeRemark实现Serializable { @Id @Column(name =EMP_REMARK_ID)私有长ID; @OneToOne @JoinColumn(name =EMP_ID)私人员工雇员; @Column(name =REMARK) private String remark; // getters and setters } 保存员工,只需致电保存员工。 EmployeeRemark会级联到所有操作,并会与员工一起被删除,或者以其他方式成为孤儿。 I need to create a table EMPLOYEE_REMARK from a table EMPLOYEE.And need to do it with Annotation Hibernate.EMPLOYEEEMP_ID, EMP_FNAME, EMP_LNAMEEMPLOYEE_REMARKEMP_REMARK_ID, EMP_ID, REMARKit will be a OnetoOne relationship i.e, for each EMP_ID there will be one REMARK. REMARK could be null.please help me with the solution...Can it be done by creating one class from employee and populate the EMPLOYEE_REMARK from it??? 解决方案 Basically here is the way of doing what you want.Employee@Entity@Table(name = "EMPLOYEE")public class Employee implements Serializable { @Id @Column(name = "EMP_ID") private Long id; @Column(name = "EMP_FNAME") private String firstName; @Column(name = "EMP_LNAME") private String lastName; @OneToOne(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true) private EmployeeRemark employeeRemark; public void setRemark(String remark) { this.employeeRemark = new EmployeeRemark(); this.employeeRemark.setRemark(remark); this.employeeRemark.setEmployee(this); } public String getRemark() { return employeeRemark == null ? null : employeeRemark.getRemark(); } //getters and setters}Employee Remark@Entity@Table(name = "EMPLOYEE_REMARK")public class EmployeeRemark implements Serializable { @Id @Column(name = "EMP_REMARK_ID") private Long id; @OneToOne @JoinColumn(name = "EMP_ID") private Employee employee; @Column(name = "REMARK") private String remark; //getters and setters}When saving employee, just call save on employee. EmployeeRemark will cascade to all operations and will be removed along with employee or if it become an orphan in other way. 这篇关于使用Hibernate Annotation从父表创建并填充子表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 02:51