简体   繁体   English

用于显示和CRUD操作的实体类

[英]Entity class for display and for CRUD operation

I am using Hibernate 4.1.0 and JPA 2.0 我正在使用Hibernate 4.1.0和JPA 2.0

I have an Entity class like the following 我有一个像下面这样的实体类

@Entity
@Table(name = "V_EMPLOYEES")
public class Employee {

        @Id
        @Column(name = "EMPLOYEENUMBER")
        private String employeeNumber;

        @Column(name = "EMPLOYEENAME")
        private String employeeName;

        @Column(name = "DEPARTMENT")
        private String dept;

/* getters and setters for the above */ / *以上的吸气器和设置器* /

... .. .....

@Table(name = "V_EMPLOYEES") Here V_EMPLOYEES is a view where I am using this to get employeename from my lookup table and join with employeeno . @Table(name = "V_EMPLOYEES")在这里V_EMPLOYEES是一个view ,在此view ,我使用它从查找表中获取employeename并与employeeno V_EMPLOYEES

I was using the above to display values in datatable and which works fine. 我使用上面的方法在数据表中显示值,并且工作正常。

I would like to create a new employee record when user clicks Add button and a popup dialog where user enters employeenumber , employeestatus , employeedepartment etc. 我想在用户单击“添加”按钮和一个弹出对话框(用户输入employeenumberemployeestatusemployeedepartment等)时创建一个新的雇员记录。

As I am using a view in my Entity class, if I want to create a new employee record, should I create a database view which can be updatable or create a new Entity class reference to Employee table? 当我在Entity类中使用view时,如果要创建新的雇员记录,是否应该创建可更新的数据库view或创建对Employee表的新Entity类引用? What if I have columns which are not part of datatable for view purpose and I would want to use for create or update record? 如果出于查看目的,我有一些列不在数据表中,而又想用于创建或更新记录,该怎么办?

What is the best approach and best practice for this? 最佳方法和最佳做法是什么?

I am using EntityManager for data fetching and for persisting(insert, update and delete) 我正在使用EntityManager进行数据提取和持久化(插入,更新和删除)

Any help is highly appeciable 任何帮助都是非常明显的

Given those requirements, I would maintain two separate entities, with common fields pushed up into a mapped superclass: 鉴于这些要求,我将维护两个单独的实体,并将公共字段推入映射的超类中:

@MappedSuperclass
public abstract class AbstractEmployee {
    @Id
    @Column(name = "EMPLOYEENUMBER")
    private String employeeNumber;

    @Column(name = "EMPLOYEENAME")
    private String employeeName;

    @Column(name = "DEPARTMENT")
    private String dept;

    // Constructor, getters, setters
}

@Entity
@Table(name = "V_EMPLOYEES")
public class ReadOnlyEmployee extends AbstractEmployee {
}

@Entity
@Table(name = "EMPLOYEES")
public class Employee extends AbstractEmployee {
    // Extra mappings here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM