So I have two entities:
Person.java
@Entity
@Table(name = "persons")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(mappedBy="person", cascade = CascadeType.REMOVE)
@JsonIgnoreProperties("person")
private Set<Address> addresses;
//getters and setters
}
Address.java
@Entity
@Table(name = "addresses")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull
@ManyToOne
@JoinColumn(name = "person_id", nullable = false)
@JsonIgnoreProperties("addresses")
private Person person;
//getters and setters
}
And later on in my code I have a personDb object (already saved in database) and then I add Address:
Address address = new Address();
address.setPerson(personDb);
address = addressRepository.save(address);
and now I have address object with person object attached to it but my personDb still doesn't have any addresses attached to it. Even if I try to get it from database once more:
personRepository.findOne(personDb.getId());
I have null where should be set of addresses. I also tried changing my annotation in Person class to something like this:
@OneToMany(mappedBy="person", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
or changing CascadeType to ALL but nothing helps. What can I do to load addresses to my personDb object after they were added to database?