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?
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;
}