I have class User and class Address. Address contains only 4 property:
publiс class Address {
private Long id;
private String country;
private String city;
private String street;
// getters and setters
}
And structure of User:
public class User {
private Long id;
private String name;
private Address homeAddress;
private Address businessAddress;
// other fields and methods
}
What type of JPA relationship to choose and how to make it through annotations in this case when one entity(User) has two(or more) fields having one’s own type of some custom class(Address)?
It would be a one-to-many relantionship if User has only one address. But what about this case? How to define correctly these objects in my system?
I tried to create another field users in Address class:
@OneToMany(mappedBy = "?")
private List<User> users;
But this’s not a solution for my problem because there’re two fields of Address type in class User. And it’s not clear that it’s necessary to define in the mappedBy annotation…
I will be grateful for any advice!
and the class address remain the same.