I have a Many to One relationship. Is it possible to select the children database id based on particular column prior saving the parent?
Here is my code:
public class Employee {
@AssociationOverrides({@AssociationOverride(name = "staffNumber"), @AssociationOverride(name = "staffNumber"),
@AssociationOverride(name = "homeAddress", joinColumns = @JoinColumn(name = "homeAddress")),
@AssociationOverride(name = "officeAddress", joinColumns = @JoinColumn(name = "officeAddress"))})
@EmbeddedId
private EmployeeId id;
}
@Data
@Embeddable
public class EmployeeId {
@Column(name = "staffNumber")
private String staffNumber;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Address homeAddress;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Address officeAddress;
}
@Data
public class Address {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Columnn(name="country")
private String country;
@Column(name="city")
private String city;
}
public interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {}
Is it possible to do save employee
employeeRepository.save(employee);
without having to select
addressRepository.findAddressByCity(city)
where I only have Address without the id (id = 0), but in the database there's a City("New York", with id 2 and "San Fransisco" with id 1)
Employee: {
EmployeeId: {
staffNumber: "123",
homeAddress: {id:0, country: null, city: "New York"},
officeAddress:{id:0, country: null, city: "San Fransisco"}
}
}