Mar
23
JPA 2.0 + Hibernate: OrphanRemoval & CascadeType.REMOVE
Here I am writing to cover up the difference between OrphanRemoval=true and CascadeType.REMOVE in JPA2.0 with hibernate.
Let say there is one to many relationship between User and Address and that can be defined in JPA as below:
public class User{ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user") public Set getAddresses() { return this.addresses; } } public class Address{ @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="userid") public User getUser() { return this.user; } } You can have more detail in this post about above entities.
Let say there is one to many relationship between User and Address and that can be defined in JPA as below:
public class User{ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user") public Set getAddresses() { return this.addresses; } } public class Address{ @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="userid") public User getUser() { return this.user; } } You can have more detail in this post about above entities.