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: Cannot delete or update a parent row: a foreign key...
PostPosted: Fri Jun 24, 2011 3:22 am 
Newbie

Joined: Wed Jul 21, 2010 10:26 pm
Posts: 10
Hi,
I am trying to update an entity called EntityOne followed by a series of delete's on another entity called EntityTwo. EntityTwo has a FK to EntityOne. While I do this operation within a same session(Spring managed OpenSessionInView)
I get:
Quote:
Cannot delete or update a parent row: a foreign key constraint fails


1. I first save EntityOne
2. I then get a List of EntityTwo linked to EntityOne
3. I remove one or more instances of EntityTwo calling session.delete()


Here is how the Entities Look like.
Code:

@Entity
@Table(name = "MODULE"))
  public class Module implements Serializable {
    private Long id;
    private String name;
  }

@Entity
@Table(name = "ENTITY_ONE"))
  public class EntityOne implements Serializable {
    private Long id;
    private String name;
  }

@Entity
@Table(name = "ENTITY_TWO"))
public class EntityTwo implements  Serializable {
    private Long id;
    private EntityOne entityOne;
    private Module module;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "ENTITY_ONE_ID") 
   public EntityOne getEntityOne() {
      return entityOne;
  }

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "MODULE_ID") 
   public Module getModule() {
   return module
  }

  }



Can someone tell me where I have gone wrong? The segment of code I used to update/delete is as follows.

[code]
Session session = getSession();
session.update(entityOne);
//Determine Removal List here
Collection<EntityTwo> listToRemove = getModulesToRemoveList(entityOne,selectedEntities);
for (EntityTwo entityTwo : listToRemove) {
session.delete(entityTwo);
}

Many thanks


Top
 Profile  
 
 Post subject: Re: Cannot delete or update - Solved
PostPosted: Fri Jun 24, 2011 4:49 am 
Newbie

Joined: Wed Jul 21, 2010 10:26 pm
Posts: 10
Hi All,

I have solved this. I hope my explanation helps out someone in a similar situation in future.


EntityTwo i.e the had this annotation:
Code:
   @ManyToOne(fetch = FetchType.LAZY,[b] cascade=javax.persistence.CascadeType.REMOVE[/b])
  @JoinColumn(name = "MODULE_ID")
   public Module getModule() {
   return module
  }

.

I failed to mention this as I hand coded the classes for the post./ This was vital to solving it. Since it had a cascadeType.REMOVE..It turns out that two delete SQL's were generated by HIbernate based on the annotation above.. The CasadeType.REMOVE tries to delete the Module entity. So I removed that cascadeType.REMOVE which in my case was not logical and not required as MODULE was a reference table and should not cascade a remove.

I guess first thing to do when we get this type of exception is the examine the entities involved and the annotations especially for cascades.

Cheers


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.