Meaningless Drivel is fun!
The moose likes Object Relational Mapping and the fly likes problem with one-to-one mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "problem with one-to-one mapping" Watch "problem with one-to-one mapping" New topic

problem with one-to-one mapping

Lavanya Rao

Joined: Aug 02, 2009
Posts: 8
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:
@Table(name = "USER")
@Proxy(lazy = true)
public class User implements Serializable
@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)
private UserProfile userProfile;


@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
@Column(name = "USERID")
@GeneratedValue(generator = "UserProfile")
private String userProfileId;

@OneToOne(mappedBy = "userProfile")
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:

User user=new USer();
UserProfile uf=new UserProfile();

but when i am trying to delete user profile from the user then its not happening:

User user=session.findByPrimaryKey(User.class,id);
//this is user is hvaing user profile

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

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

Thanks in advance
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3902

Please UseCodeTags.

[How to ask questions] [Twitter]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Instead of saving a null user profile, why not just issue a direct delete on the UserProfile?

-Cameron McKenzie
Lavanya Rao

Joined: Aug 02, 2009
Posts: 8
yes, i can do like that also. but why its not wroking like this?? when i am trying to add its happening , when i am deleting the User its getting deleted. but when i am setting null objevt in side User then its not firing update query...

is any thing wrong or it wont work like that or what...

Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
Try to set the the UserProfiles User to null before saving:

Hibernate DAOs with Spring | Separation of Concerns | Eclipse Code Templates | more...
Lavanya Rao

Joined: Aug 02, 2009
Posts: 8
I tried as above metioned, but its not working. It's not at all firing any query

User user=session.findByPrimaryKey(User.class,id);

no use :-(

Is any thing wrong in the mapping??
Lavanya Rao

Joined: Aug 02, 2009
Posts: 8
please any body do reply to my query..

i want to implement it is in my proj....
this is the only out standing item i have....

Thanks in advance,
I agree. Here's the link:
subject: problem with one-to-one mapping
It's not a secret anymore!