I have 2 classes:
@Entity
@Table(name = "users")
public class User {
//Some fields
@OneToMany(mappedBy = "user", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@ToString.Exclude
@EqualsAndHashCode.Exclude
private List<Order> orders;
}
And one more:
@Entity
@Table(name = "orders")
public class Order extends BaseEntity{
// Some fields
@ManyToOne(optional = false)
@JoinColumn(name = "user_id", nullable = false)
private User user;
}
So, my problem that if i try to write new user into database using method persist(), user's(Already become persisted entity with id in persistence context) field orders stays null. And it is a problem when i try to read this field in JSP or update user in database using merge() because of pushing NULL orders field
If i make this.orders = new ArrayList<>();
for every new user, all should be good. If there any possibility not to make initialization of List or do it automatically by Hibernate?