-->
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: I cant figure out how to get merge to delete orphaned object
PostPosted: Fri Jun 10, 2011 9:24 pm 
Newbie

Joined: Thu May 19, 2011 12:59 am
Posts: 13
I have a parent class such as this:

@Entity(name="parent")
@Table(name = "PARENT")
public class Parent {

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="PARENT_ID")
@Cascade({org.hibernate.annotations.CascadeType.ALL})
private List<Child> children;
}

Then what I do in my application is I load a new instance of Parent from an XML message. I discover that an equivalent record of this Parent already exists (using secondary columns) so I load the existing Parent from the DB. I set the new Parent's PARENT_ID to the existing one's PARENT_ID, and I go through the children looking for matches however in this case no matches I the new Parent's children collection remains empty.

I then call HibernateTemplate.merge(NewParent)

to merge my new parent instance to the database.

I expect it to delete the orphaned children, instead in the SQL it's trying to update the children record's primary key to NULL! Which of course Oracle does not allow and throws an error:

update
CHILD
set
PARENT_ID=null
where
PARENT_ID=?

ORA-01407: cannot update ("CHILD"."PARENT_ID") to NULL


What did I do wrong?

I hope I dont have to take my new parent coming in from XML, and then copy every single property to the Parent object I loaded from the database, then call save or merge on the existing parent. I thought Hibernate makes this easy?


Top
 Profile  
 
 Post subject: Re: I cant figure out how to get merge to delete orphaned object
PostPosted: Sat Jun 11, 2011 11:44 am 
Newbie

Joined: Thu May 19, 2011 12:59 am
Posts: 13
I got it working now, however not sure if I am going overkill with all these annotations!

Also, it looks like I HAVE to use Set<> instead of List<> - bummer!!!

On my parent class I annotate the collection of children as follows:

@OneToMany(mappedBy="parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="PARENT_ID", nullable=false)
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN}

On my child class I annotate the bidrectional reference to parent as follows:

@ManyToOne(targetEntity = Parent.class, fetch=FetchType.LAZY)
@JoinColumn(name="PARENT_ID", nullable=false)

Does that all make sense? Is there any excessive, non-functional annotation I can trim off?


Top
 Profile  
 
 Post subject: Re: I cant figure out how to get merge to delete orphaned object
PostPosted: Sat Jun 11, 2011 12:52 pm 
Newbie

Joined: Thu May 19, 2011 12:59 am
Posts: 13
Ok think I am finally making sense out of all of this!

I can use a List<> however if I use multiple Lists together with EAGER fetching- it's a problem! Get a multiple bags error.

Also I think all I need to annotate is:

@OneToMany(mappedBy="parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN}

on the parent

And:

@ManyToOne(targetEntity = Parent.class, fetch=FetchType.LAZY)

on the child.


Top
 Profile  
 
 Post subject: Re: I cant figure out how to get merge to delete orphaned object
PostPosted: Thu Jun 16, 2011 12:48 am 
Newbie

Joined: Thu Jun 16, 2011 12:03 am
Posts: 3
Hi -

I am also facing the same problem. I appreciate if you could post me parent and child code. I tried what you have done but still getting errors.

Thanks


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.