1

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"}
   }
}
user293655
  • 508
  • 1
  • 6
  • 23
  • You want to select child table record based on child table column name ? Please specify more details exactly what you want. – Naresh Kumar Dec 26 '19 at 10:41
  • I would like to save the parent along with the children. I would like to do `employeeRepository.save(employee);` without having to search what is the Address id first with a result in db as below ``` Employee: { EmployeeId: { staffNumber: "123", homeAddress: id:2, --> 2 is the Address table id officeAddress: id:1 --> 1 is the Address table id } } ``` – user293655 Dec 26 '19 at 10:42
  • You mean you want to save new employee with new address? – Naresh Kumar Dec 26 '19 at 12:11
  • Please check this [hibernate relation](https://stackoverflow.com/questions/3927091/save-child-objects-automatically-using-jpa-hibernate) – Naresh Kumar Dec 26 '19 at 13:08
  • Is it possible to save the child without knowing the child's id first? But I know some of the child's columns? – user293655 Dec 26 '19 at 20:21

1 Answers1

1

You can save a child record without knowing the child primary id, but make sure the result should be unique if you fetching other than the primary key column.

Employee employee = new Employee();
employee.setStaffNumber("123);

You can find an address by other columns also like below.

Address officeAddress = addressRepository.findAddressByCity(city); // Office Address
Address homeAddress = addressRepository.findAddressByCity(city); // Home Address

// Setting child objects in parent object
employee.setHomeAddress(homeAddress);
employee.setOfficeAddress(officeAddress);

And finally, save the parent object it will save employee detail with home and office address.

employeeRepository.save(employee);
Naresh Kumar
  • 794
  • 8
  • 25
  • Thanks Naresh. I was hoping that I don't have to do `findAddressByCity` first to set the children. I was hoping there's some annotation in Adrress to query maybe something like `@Query("SELECT id FROM Address WHERE city =$")` – user293655 Dec 30 '19 at 00:34