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