I have two entities: User and Action related One user to many Actions by ID.
I created instances of them and want to insert them to DB.
It's okay if I insert the User first and then the Action. But if I insert Action before insert User i will get deadlock because Action waiting for realted User will be inserted.
How to check if User was inserted before insert Action entity?
Code example:
@Entity
class User{
@Id
Integer userID;
@OneToMany(mappedBy = "actions", targetEntity = Action.class, cascade = CascadeType.ALL)
List<Action> actions = new ArrayList<>();
//getters and setters
}
@Entity
class Action{
@Id
Integer actionID;
@ManyToOne(fetch = FetchType.EAGER, targetEntity = User.class)
@JoinColumn(name = "user_id", nullable = false)
User user;
//getters and setters
}