-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: hibernate does not delete the child entity
PostPosted: Fri Jun 25, 2010 12:55 pm 
Newbie

Joined: Mon May 24, 2010 10:48 pm
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: hibernate does not delete the child entity
PostPosted: Wed Aug 25, 2010 11:03 am 
Newbie

Joined: Thu Mar 17, 2005 10:41 pm
Posts: 3
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.