• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't remove (delete) rows in one to many relationship

 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem trying to delete rows in a one to many relationship:

my parent:

// Set of PerformanceRatings defined in employeeMaster

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "employeeMaster")
public Set<PerformanceRating> getPerformanceRatings () {
return this.performanceRatings;
}

// parent reference in performanceRating entity:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Employee_ID", nullable = false)
public EmployeeMaster getEmployeeMaster() {
return this.employeeMaster;
}

When I try to delete (remove) a child entity (performanceRating) my next query will fail with 'trying to reference a deleted entity' even if I try to merge the parent after the remove. Same if I try to save the parent. How can I delete a performanceRating entity and keep the parent in sync?
I am having a similar problem in create performanceRating - updates are not seen until the next session.

thank you
Max


 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you delete an object in a OneToMany you must also remove it from the OneToMany collection.
Alternatively you can use orphanRemoval in JPA 2.0, and just remove the object which will trigger the deletion.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side

 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the answer, James-
After much trial and error, the only way I could get this to work was by deleting the child rows with a delete query on the child table only - not referencing the parent.
Then it worked.
max
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Orphan removal is possible with JPA 2.0 or Hibernate in earlier versions. But I don't think this is the problem.

You need to care for the object consistency yourself, the ORM tools can't do this for you. So when you have bidirectional relations you need to ensure the two relations, which are unrelated for the ORM tools, are consistent. Means: When you delete a child entity from a collection you need to clear the back reference, the parent reference; when adding a child you have to set it. So you don't do a parentObject.getChilds()/*returns a collection*/.add(new Child()) but you would write addChild/removeChild methods that adds the child to a private collection and sets the parent reference in the child object to this or null. When the object structure is inconsistent then the ORM can't do much.

It is much easier when you try to avoid bidirectional relations unless they are needed.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic