I have got three entities, Session, Order and User (part of my online movie tickets project). In my domain model, Order keeps fk of both User and Session. As you can see in my code:
@Table(name="Orders")
@Entity
public class Order {
@ManyToOne
@JoinColumn(nullable = false)
private User user;
@ManyToOne
private Session session;
...
}
@Entity
@Table(name="Session")
public class Session {
@OneToMany(fetch=FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "session")
private List<Order> orders = new ArrayList<Order>();
...
}
@Table(name="User")
@Entity
public class User {
@OneToMany(cascade = { CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.REMOVE },
mappedBy = "user")
private @Getter Set<Order> orders = new HashSet<>();
...
}
My question is, Can I use CascadeType.ALL
in both Session and User?
Are there potential conflicts when update Order with both Session and User?
As you can see, I use fetchType.Lazy, Can it guarantee that orders in both Session and User are up-to-date?