Java development in the same object multiple times to the same primary key attribute of an object as @JoinColumn comment

Java development process, often encounter a situation object repeatedly used the primary key of another object as an attribute. For example, a person's record department changes, we need to record sector numbers before and after the change. Before a misunderstanding, that in this case must be the primary key attribute @JoinColumn entity class, and in fact the value of this attribute is a foreign key of the current annotation entity classes. For example as follows:

The preferred class entity created Department

@Entity
@Table(name="department")
public class Department implements Serializable{

    private static final long serialVersionUID = 1L;
    
    private int departmentId;

    private String departmentName;
    
    @Id
    @GenerateValue
    @Column(name = "departmentId",nullable = false)
    public int getDepartmentId(){
            return departmentId;    
    }

    public void setDepartmentId(int departmentId) {
	    this.departmentId = departmentId;
    }

    @Column(name = "departmentName",nullable = false)
    public int getDepartmentName(){
            return departmentName;    
    }

    public void setDepartmentName(String departmentName) {
	    this.departmentName = departmentName;
    }

}

    

  And Employee entity class

@Entity
@Table(name="employee")
public class Employee implements Serializable{

    private static final long serialVersionUID = 1L;
    
    private int employeetId;

    private String employeeName;
    
    @Id
    @GenerateValue
    @Column(name = "employeeId",nullable = false)
    public int getEmployeeId(){
            return employeeId;    
    }

    public void setEmployeeId(int employeeId) {
	    this.employeeId = employeeId;
    }

    @Column(name = "employeeName",nullable = false)
    public int getEmployeeName(){
            return employeeName;    
    }

    public void setEmployeeName(String employeeName) {
	    this.employeeName = employeeName;
    }

}

  Next, create a user's mobilization department, to achieve the following:

@Entity
@Table(name="empDeptChangeInfo")
public class EmpDeptChangeInfo implements Serializable{

    private static final long serialVersionUID = 1L;

    private int itemId;
  
    private Employee employee;

    private Department dept1;

    private Department dept2;

    @Id
    @GenerateValue
    @Column(name = "itemId",nullable = false)
    public int getItemId(){
         return itemId;    
    }

    public void setItemId(int itemId) {
	 this.itemId = itemId;
    }

    @ManyToOne(cascade = CascadeType.REFRESH, optional = true)
    @JoinColumn(name = "employeeId")
    public int getEmployeeId(){
       return employeeId; 
    }
    
    public void setEmployee(Employee employee){
         this.employee = employee;
    }

    @ManyToOne(cascade = CascadeType.REFRESH, optional = true)
    @JoinColumn(name = "deptId1")
    public getDept1(){
         return dept1;
    }
    
    public void setDept1(Department dept1){
         this.dept1 = dept1;
    }

    @ManyToOne(cascade = CascadeType.REFRESH, optional = true)
    @JoinColumn(name = "deptId2")
    public getDept2(){
         return dept2;
    }
    
    public void setDept2(Department dept2){
         this.dept2 = dept2;
    }
}

  Notably, the latter @ JoinColumn property not associated with the name of the database field name or attributes defined in the table, and as Dept1 Dept2 empDeptChangeInfo corresponding table in the database field and deptId1 platform deptId2, and not the department table fields departmentId.

  

 

Guess you like

Origin www.cnblogs.com/snowcity999/p/10939547.html