pamlwong wrote:
hibernate does is delete the association in the join table but it does not delete the child entity.
My Entity Mapping
public class Role {
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<UserAuthorities> userAuthoritiesList;
}
public class UserAuthorities {
@Id
@SequenceGenerator(name = SEQUENCE, sequenceName = "xx_id_seq")
@GeneratedValue(strategy = GenerationType.AUTO, generator = SEQUENCE)
@Column(name = "id", nullable = false)
private Long userAuthoritiesId;
@ManyToOne
@JoinColumn(name = "role_id")
private Role role;
}
Everytime when a user maintain a role, he/she can change the user authorities of the role. for my case, before saving the role record to database, i will clear the userAuthoritiesList and re-set a new userAuthoritiesList to the role.
when i trying to save, i realise hibernate will remove the association to the UserAuthorities instead of delete the child entity.
Example:
Before Save in my user authorities table:
auth_id | role_id
1 1
2 1
3 1
After i reassign 4 authorities to the role & SAVE, it become
role_id | auth_id
1
2
3
4 1
5 1
6 1
7 1
But it suppose the remove the 1st 3 records from user authorities. i thought @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) suppose to be working on this.
Please advise
Lazy setting is a big problem,I met this as you yesterday.
if you change lazy to true,you can really delete orphan objects.
or another solution,when setting lazy=false,after open session,call Hibernate.initialize() to manually load associations,and then remove them from the list.
with these tow way,maybe you can work out. I think it seem like a Hibernate's bug.
look this issue for more track:http://opensource.atlassian.com/projects/hibernate/browse/HHH-2344