-->
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: Merging a @OneToOne relationship creates orphaned records
PostPosted: Wed Dec 06, 2006 10:48 am 
Newbie

Joined: Wed Nov 29, 2006 8:13 pm
Posts: 8
I have an Entree object that contains a one-to-one relationship with a Ticket object, and this relationship is annotated with the @OneToOne(cascade = CascadeType.ALL) on the Entree side. I first insert this object into the db with hydration. The tables look like this--

Code:
ENTREE

entree_id | name | ticket_id
-----------------------
1         | DS 1 | 1


TICKET

ticket_id | number
---------------------
1         | 213


When I retrieve this object graph and disassociate with the session, I make changes to the graph like this-- entree.getTicket().setNumber(214).

Now I would expect that when I call session.merge(entree) that this would create/delete the changed record like this in the db--


Code:
ENTREE

entree_id | name | ticket_id
-----------------------
1         | DS 1 | 2


TICKET

ticket_id | number
---------------------
2         | 214


However, Hibernate does not delete the old record and thereby making it orphaned. This is what actually happened when I execute my code--

Code:
ENTREE

entree_id | name | ticket_id
-----------------------
1         | DS 1 | 2


TICKET

ticket_id | number
---------------------
1         | 213
2         | 214


Notice that the record with ticket_id = 1 is now orphaned. Since CascadeType.DELETE_ORPHAN is only used for one-to-many associations, how do I fix this so that there are no orphaned records for one-to-one relationships after a merge()? Below is my code

Code:
public class Entree {
   private Long id;
   private Ticket ticket;
   private String name;
 
   @Id
   public Long getId() {
     return this.id;
   }

   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="ticket_id")
   public Ticket getTicket() {
     return this.ticket;
   }

  // appropriate setters
}


public class Ticket {
   private Long id;
   private String number;

   @Id
   public Long getId() {
     return this.id;
   }

   ...
}


-Larry


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 6:32 pm 
Newbie

Joined: Wed Nov 29, 2006 8:13 pm
Posts: 8
I tried a lot of possibilities to figure out the cause of the orphaned records but with no success. Is this the expected behavior of Hibernate (3.2.0.ga) for one-to-one associations during a merge or is this a bug? I would assume that for any one-to-one relationship, the child side would always be changed (if needed) w/o the need to create a new record. Does anyone have any hints or suggestions on what I may be doing wrong?

-Larry


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.