[英]Spring Boot: How to create similar another entity by using existing entity class
I'm developing one simple app where where I have an one entity class class Employee.我正在开发一个简单的应用程序,其中我有一个实体 class class Employee。 And now I want to create/copy new similar entity called ActiveEmployees from existing Employee.
现在我想从现有员工创建/复制名为 ActiveEmployees 的新类似实体。 I want to add functionality that If I hit the new api endpoint ->POST: http://locahost:8080/api/employee/active/john -> So, it should save existing Employee John Record in the new table active_employees with the all Table data.
我想添加功能,如果我点击新的 api 端点 -> POST: http://locahost:8080/api/employee/active/john -> 因此,它应该将现有的员工 John Record 保存在新表 active_employees 中所有表数据。
@Entity
@Table(name="employee")
public class Employee{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
@NotNull
private String firstNname;
@Column
@NotNull
private String lastNname;
@Column
@NotNull
private String department;
@JsonManagedReference
@OneToOne(fetch = FetchType.LAZY,
mappedBy = "employee",
cascade = CascadeType.ALL,
orphanRemoval = true)
ActiveEmployee activeEmployee;
... Constructor, getters and setters
}
@Entity
@Table(name="active_employees")
public class ActiveEmployees {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JsonBackReference
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id")
private Employee employee;
}
I think you should use inheritance mapping in hibernate instead having two table with same fields.我认为您应该在 hibernate 中使用 inheritance 映射,而不是使用两个具有相同字段的表。 There are multiple strategies.
有多种策略。 Check and use best one which fits your requirement.
检查并使用最适合您要求的。
Read the tutorial here https://www.javatpoint.com/hibernate-inheritance-mapping-tutorial在此处阅读教程https://www.javatpoint.com/hibernate-inheritance-mapping-tutorial
You can use inhertiance with @MappedSuperclass.您可以将继承与 @MappedSuperclass 一起使用。 But if I will design this application I will add boolean field "active" to Employee class.
但是,如果我要设计这个应用程序,我会将 boolean 字段“活动”添加到员工 class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.