I have the following classes
public class User {
@OneToMany(
fetch = FetchType.EAGER,
mappedBy = "user",
cascade = CascadeType.ALL
)
private Set<UserSession> sessions;
public UserSession login() {
UserSession session = new UserSession();
session.setUser(this);
session.persistAndFlush();
this.persistAndFlush();
return session;
}
....
public class UserSession {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
protected User user;
as you see, I am adding session from it's side. Sometimes in other times I am getting
caused by: org.hibernate.HibernateException: collection was evicted
How to do correctly?
To add the element in Bi-Directional relationship,
Sample code:
Fully working code: