简体   繁体   English

Spring 引导:如何使用现有实体 class 创建类似的另一个实体

[英]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.

相关问题 Spring 开机 | 在实体 class 中创建另一个实体 class object - Spring Boot | Making another Entity class object in Entity class 如何使用 Spring 引导为传记后端制作实体 class 和 controller? - How to make entity class and controller for biography backend using Spring Boot? 如何使用非实体表在 Spring Boot 上创建自定义 @Query? - How to create a custom @Query on Spring Boot using a non Entity table? 如何使用 spring 引导从数据库创建实体类? - How to create entity classes from database using spring boot? Spring 引导:如何使用复合键创建实体 - Spring Boot: How to create an Entity with a composite key 如何在 java Z91F1F487C7FB763BF84FEZB3EB9 中的抽象 class 和具体 class 之间创建实体关系 - How to create an entity relationship between an abstract class and a concrete class in a java spring-boot app? 在另一个Spring实体中使用Spring实体 - Using a Spring Entity inside another Spring Entity 如何从实体类 spring boot 生成 CRUD 存储库类 - How to generate CRUD repository class from entity class spring boot 如何在Spring Boot中为只有外键的表映射/创建实体类 - How do I map/create an entity class for a table which has only foreign keys in Spring Boot Spring boot 如何编辑实体 - Spring boot how to edit entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM