简体   繁体   中英

Entity class for display and for CRUD operation

I am using Hibernate 4.1.0 and 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 .

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.

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? 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)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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