'How do i join between 2 entities with column other than primary key using annotations?
I have 2 tables Employee and Address. Each have primary key empId and addressId which is generated through sequence.
Employee Class:
public class Employee {
@Id
@Column(name = "sequence_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emp_seq")
@SequenceGenerator(sequenceName = "emp_seq", allocationSize = 1, name = "emp_seq")
private Long sequenceId;
@Column(name = "emp_id")
private String empId;
@Column(name = "joiningDate")
private Date businessDate;
@OneToOne(mappedBy = "employee", cascade = CascadeType.ALL)
private Address address;
}
Address Class
public class Address {
@Id
@Column(name = "address_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "address_seq")
@SequenceGenerator(sequenceName = "address_seq", allocationSize = 1, name = "address_seq")
private Long addressID;
@Column(name = "street")
private String street;
@OneToOne
@JoinColumn(name = "emp_id")
@JsonIgnore
private Employee employee;
}
I am using JPARepository to save both entities. here is my main class.
Employee e = Employee.builder().businessDate(new Date())
.empId("thinkerId").build();
Address a = Address.builder().street("32nd Street")
.employee(e).build();
e.setAddress(a);
empRepository.save(e);
Code runs successfully. But in my Address table, emp_Id column contains the primary key instead of the emp_id column.
How can i get "thinkerId" as value in Address table?
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
