-->
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: OneToOne Mapping Doesn't Delete Orphans
PostPosted: Tue May 05, 2009 5:01 am 
Newbie

Joined: Mon Dec 29, 2008 9:48 am
Posts: 6
Hi.
I have a class User which holds another class UserHistory (for additional data) in OneToOne mapping. When I persist User, a new record for UserHistory is automatically created. The problem is that when I set a new instance of UserHistory in User and persist User, the old UserHistory record is not deleted, even though no one points at it anymore.

Here is some sample code (with simpler classes) of the problem:

MyUser class:
Code:
@Entity
@Table(name = "MY_USER_TABLE")
public class MyUser
{
   @Id
   @Column(name = "MY_USER_ID")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Integer id = null;

   @OneToOne(cascade = CascadeType.ALL)
   @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private UserHistory userHistory = null;

// getters and setters...
}


UserHistory class:
Code:
@Entity
@Table(name = "USER_HISTORY_TABLE")
public class UserHistory
{
   @Id
   @Column(name = "USER_HISTORY_ID")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Integer id = null;

   private String name = null;

// getters and setters...
}


The persisting code:
Code:
      MyUser user = new MyUser();
      UserHistory originalData = new UserHistory("Original Data");
      user.setHeldData(originalData);
      session =startNewSession();
      session.saveOrUpdate(user);
           closeSession(session);
      
      UserHistory newerData = new UserHistory("Newer Data");
      user.setHeldData(newerData);
      session = startNewSession();
      session.saveOrUpdate(user);
           closeSession(session);


After the second "saveOrUpdate" the UserHistory table contains both records, even though the first one has just become orphan.

Is there an automatic way to get hibernate to delete such records (or maybe another way, not using OneToOne, to keep UserHistory in MyUser so that is deleted automatically - note that I cannot @Embedded since my real classes are far more complicated)?

Thanks,
Oz


Top
 Profile  
 
 Post subject: Re: OneToOne Mapping Doesn't Delete Orphans
PostPosted: Tue May 05, 2009 5:25 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Quote:
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
cascade option is only for collection (set,list,map). not for single entity association. so when you are setting new UserHistory object,it is not deleting previous one (using ORPHAN) setting.

(As you can't @Embedded the UserHistory , so UserHistory will treat as @Entity and its life cycle will be independent to MyUser.) So you can't delete UserHistroy with the help of annotation. you have to manually delete it.

One better way is rather than deleting a UserHistory and setting new UserHistory object in MyUser, update the values in existing UserHistory association.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.