Skip to main content

JPA OneToMany not deleting child


i have a problem with a simple @OneToMany mapping between a parent and a child entity. All works well, only that child records are not deleted when i remove them from the collection.



The parent:




@Entity
public class Parent {
@Id
@Column(name = "ID")
private Long id;

@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "parent")
private Set<Child> childs = new HashSet<Child>();

...
}



The child:




@Entity
public class Child {
@Id
@Column(name = "ID")
private Long id;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="PARENTID", nullable = false)
private Parent parent;

...
}



If i now delete and child from the childs Set, it does not get deleted from the database. I tried nullifying the child.parent reference, but that did not work either.



The entities are used in a web application, the delete happens as part of an ajax request. I don't have a list of deleted childs when the save button is pressed, so i can't delete them implicitly.



Any pointers? Thanks in advance for your time


Source: Tips4allCCNA FINAL EXAM

Comments

  1. JPA's behaviour is correct (meaning as per the specification): objects aren't deleted simply because you've removed them from a OneToMany collection. There are vendor-specific extensions that do that but native JPA doesn't cater for it.

    In part this is because JPA doesn't actually know if it should delete something removed from the collection. In object modeling terms, this is the difference between composition and "aggregation*.

    In composition, the child entity has no existence without the parent. A classic example is between House and Room. Delete the House and the Rooms go too.

    Aggregation is a looser kind of association and is typified by Course and Student. Delete the Course and the Student still exists (probably in other Courses).

    So you need to either use vendor-specific extensions to force this behaviour (if available) or explicitly delete the child AND remove it from the parent's collection.

    I'm aware of:


    Hibernate: cascade delete_orphan. See 10.11. Transitive persistence; and
    EclipseLink: calls this "private ownership". See How to Use the @PrivateOwned Annotation.

    ReplyDelete
  2. Thanks to both your replies. I answer myself to post the code that solved my problem. As explained, it is not possible to do what i want with JPA, so i employed the hibernate.cascade annotation, with this, the relevant code in the Parent class now looks like this:

    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, mappedBy = "parent")
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
    org.hibernate.annotations.CascadeType.DELETE,
    org.hibernate.annotations.CascadeType.MERGE,
    org.hibernate.annotations.CascadeType.PERSIST,
    org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    private Set<Child> childs = new HashSet<Child>();


    I could not simple use 'ALL' as this would have deleted the parent as well.

    Thanks again for your answers.

    ReplyDelete
  3. In addition to cletus' answer, JPA 2.0, final since december 2010, introduces an orphanRemoval attribute on @oneToMany annotations.
    For more details see this blog entry.

    Note that since the spec is relatively new, not all JPA 1 provider have a final JPA 2 implementation. For example, the Hibernate 3.5.0-Beta-2 release does not yet support this attribute.

    ReplyDelete
  4. Here cascade, in the context of remove, means that the children are removed if you remove the parent. Not the association. If you are using Hibernate as your JPA provider, you can do it using hibernate specific cascade.

    ReplyDelete
  5. I Think you may set the values for parent domain object & try to delete that from relevant class..

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex