简体   繁体   中英

Using Spring Boot, how can I create the relationship between 2 entities on a new table and give it extra columns?

I have this two entities:

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "Workplace")
public class Employee {

    @Id
    @GeneratedValue
    int id;

    String name;

    String dni;

    java.time.LocalDate startDate;

}

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "Workplace")
public class Workplace {

    @Id
    @GeneratedValue
    int id;

    String code;

    String location;
}

One workspace can have many employees. I need to store the relationship in a new table (lets call it Contract ) and I need it to have the following fields:


    int idEmployee;
    
    int idWorkplace;

    java.time.LocalDate startDate;
    
    java.time.LocalDate endDate;

The field startDate must be obtained from the Employee, but the endDate will be empty by default.

How can I achieve this?

You need to manualy create it

@IdClass(ContractId.class)
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "Contract")
public class Contract {

     @Id
     private int idEmployee;
     @Id
     private int idWorkplace;

     private java.time.LocalDate startDate;

     private java.time.LocalDate endDate;

     @OneToOne
     Employee employee

}

Then you need that composite key also

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContractId implements Serializable {

     private int idEmployee;
     private int idWorkplace;

  }

Then your relevant classes need some extra modification on that relationships

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "Workplace")
public class Workplace {

    @Id
    @GeneratedValue
    int id;

    String code;

    String location;

    @OneToMany(mappedBy = "idWorkplace")
    private List<Contract> contracts;
}

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "Workplace")
public class Employee {

    @Id
    @GeneratedValue
    int id;

    String name;

    String dni;

    java.time.LocalDate startDate;

    @OneToOne(mappedBy = "idEmployee")
    Contract contract
}

Then for your requirement

The field startDate must be obtained from the Employee, but the endDate will be empty by default.

You either deal with it manually when persisting those contract entities

or

remove it completely from Employee and let it be only in Contract. That would be best practice for me.

I've found the way to do it:

@Getter
@Setter
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class MyOtherTable {
    @Id
    @GeneratedValue
    private Integer id;
    @OneToOne
    private Workplace workplace;
    @OneToOne
    private Employee employee;
    private String otherProperty;
}

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "Employee")
public class Employee {
    @Id
    @GeneratedValue
    private int id;    
    private String name;
    private String dni;
    private java.time.LocalDate startDate;

    @OneToOne                   
    private WorkPlace workplace;
}

I am not sure why you would like to create a new table for one to many relationship. Generally we create new table only where there is a many to many relationship. When we have many-to-many relationship we created third relationship table with composite primary key. why you need to create a third relationship table for one to many.

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