Two points
(1) The relationship annotations such as @javax.persistence.OneToMany, @javax.persistenece.ManyToOne supports the argument "cascase", it supports the values such as CascadeType.PERSIST, CascadeType.MERGE, etc.
When the cascade contains "CascadeType.PERSIST", The other related object(s) will be saved too automatically when the current object is saved.
(2) In bidirectional, one side must be real side and the other side must be fake side.
You can choose
Code:
public class A {
@OneToMany(mappedBy = "a") //Fake side
private Set<B> bs;
}
public class B {
@ManyToOne
@JoinColumn(...) //Real side
private A a;
}
or
Code:
public class A {
@OneToMany(mappedBy = "a")
@JoinColumn(...) //Real side
private Set<B> bs;
}
public class B {
@ManyToOne
@JoinColumn(..., insertable = false, update = false) //FakeSide
private A a;
}
,
No choice! only the changing on the real side can be use to change the relationship in database.