-->
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.  [ 4 posts ] 
Author Message
 Post subject: one-to-one bidirectional delete problem
PostPosted: Fri Aug 07, 2009 1:10 am 
Newbie

Joined: Fri Aug 07, 2009 1:02 am
Posts: 4
Hi,
i am getting problem with one-to-one mapping. i have mapped it as birectional, the child table is sharing PK of parent as PK.

I am using using hiber nate annotations 3.3 and spring 2.0


here is the mapping:
Code:
@Entity
@Table(name = "USER")
@Proxy(lazy = true)
public class User implements Serializable
{
@Id
@Column(name = "USER_ID", nullable = false, length = 50)
@GeneratedValue(generator = "uuid.hex")
@GenericGenerator(name = "uuid.hex", strategy = "uuid.hex")
private String userId;
...
@OneToOne(fetch = FetchType.LAZY)
@Cascade({CascadeType.ALL})
@PrimaryKeyJoinColumn
private UserProfile userProfile;
}


Code:

@Entity
@Table(name = "USER_PROFILE")
@org.hibernate.annotations.GenericGenerator(name = "UserProfile", strategy = "foreign", parameters = {@org.hibernate.annotations.Parameter(name = "property", value = "userProfile")})
public class UserProfile implements Serializable
{
@Id
@Column(name = "USERID")
@GeneratedValue(generator = "UserProfile")
private String userProfileId;

@OneToOne(mappedBy = "userProfile")
@SuppressWarnings("all")
private User user;
}

this is the mapping, it is hvaing normal setter/getter methods.

my problem is::

when i am adding user profile through user it is getting added:
Code:
User user=new USer();
...
UserProfile uf=new UserProfile();
uf.serUser(user);
user.setUserProfile(uf);
...
session.save(user);


but when i am trying to delete user profile from the user then its not happening:
Code:
User user=session.findByPrimaryKey(User.class,id);
//this is user is hvaing user profile
user.setUserProfile(null);
..
session.save(user);
..


but the deletion is not happening... :-(

please help me out...what is the problem??

Thanks in advance


Top
 Profile  
 
 Post subject: Re: one-to-one bidirectional delete problem
PostPosted: Mon Aug 10, 2009 7:23 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Ahh, setting it to null is problemsome.

What about just getting the object, user.getUserProfile(); and then just issuing a session.delete(userProfile);

Any chance?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: one-to-one bidirectional delete problem
PostPosted: Tue Aug 11, 2009 1:29 am 
Newbie

Joined: Fri Aug 07, 2009 1:02 am
Posts: 4
hmm...probelme with Hibernate or my mapping???


yes..its happening if i am deleting indidvidually means::

Code:
//working fine
session.delete(userProfile);
//if user has profile then its deletion that also
session.delete(user)
//get the user from session
UserProfile uf=user.getUserProfile();
//update the user profile
uf.setName("some thg");
//then update the parent means user
session.update(user);



all above operations are working fine except my requirement :-(
why its not deleting ...if it is updating then it shoud delete also , but not happening..

Pleae help me out..


Top
 Profile  
 
 Post subject: Re: one-to-one bidirectional delete problem
PostPosted: Tue Aug 11, 2009 8:21 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, the null seems to imply a detaching of the relationship, but not delete the associated instance. Delete the entity itself, that's the way to approach it.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.