JPA, Eclipselink and storing hierarchies

Yesterday we worked on the new storage facility for the Epos 0.9 release. The new release will be based on EclipseLink and its JPA implementation. The new Tree store will not be String based (as in 0.8), but provide a better representation for trees using nested sets. There we discovered a problem with tree nodes and cascading their removal. We basically provide two entities to store trees. The TreeEntity and a Node entity. The theory behind that is quiet simple. Basically the Tree hoilds a reference to the root Node (curently it holds references to all Nodes in the Tree) and a Node has a reference to its parent node and to all children.

Here is the basic structure for the TreeEntity


@Entity
public class TreeEntity {
    @OneToMany(cascade={CascadeType.ALL})
    private List nodes;
...
}

and for the nodes


@Entity
public class Node{
    @ManyToOne()    
    private Node parent;
    @OneToMany(cascade={CascadeType.ALL},mappedBy="parent")
    private List children;
...
}

The interesting (and problematic) part is the cascading. When we delete a tree, we want to delete all nodes and when deleting a node, we want all children to be deleted as well. And that works only in theory :) The Problem we discovered seems to be related to a Bug in EclipseLink, but I am not 100% sure about that. What I am sure about is that it is related to the delete order. We have a demo tree with just a root and one child. Successfully deleting the tree results in the following log entry:

--The remove operation has been performed on: test.Tree[id=1]
--The remove operation has been performed on: test.Node[id=2]
--The remove operation has been performed on: test.Node[id=3]
--begin unit of work commit
--begin transaction
--Execute query DeleteObjectQuery(test.Node[id=3])
--DELETE FROM NODE WHERE (ID = ?)
	bind => [3]
--Execute query DeleteObjectQuery(test.Node[id=2])
--DELETE FROM NODE WHERE (ID = ?)
	bind => [2]
--Execute query DeleteObjectQuery(test.Tree[id=1])
--DELETE FROM TREE WHERE (ID = ?)
	bind => [1]
--commit transaction
--end unit of work commit

First, the child node with ID 3 gets deleted, than the root with ID 2 and finally the tree object with ID 1. Note that we didn’t invoke the Node deletion explicitly. We just removed the tree instance. The same call fails in some situations with this log:

--The remove operation has been performed on: test.Tree[id=1]
--The remove operation has been performed on: test.Node[id=2]
--The remove operation has been performed on: test.Node[id=3]
--begin unit of work commit
--begin transaction
--Execute query DeleteObjectQuery(test.Node[id=2])
--DELETE FROM NODE WHERE (ID = ?)
	bind => [2]
--VALUES(1)
--Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 1.0.2 (Build 20081024)): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: DELETE on table 'NODE' caused a violation of foreign key constraint 'FK_NODE_PARENT_ID' for key (2).  The statement has been rolled back.
Error Code: 20000
Call: DELETE FROM NODE WHERE (ID = ?)
	bind => [2]
Query: DeleteObjectQuery(test.Node[id=2])
	at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:313)

The first thing that happens after calling commit on the transaction is a delete call to the root node with ID 2. That results in the foreign key vialoation because the child node still exists and references the root in its parent field. I was only able to reproduce that behavior on Derby DB in embedded mode – and it does not happen always. Strange !
To solve the problem, we followed the advice in the bug report and added a @PrivateOwned annotation to the Node’s child list while disabling the remove cascade


...
@OneToMany(cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST}, mappedBy="parent")
@PrivateOwned
private List children;
...

That did the trick. I still hope that the problem gets fixed in EclipseLink and we can go back to default JPA spec instead of relying on a EclipseLink specific annotation. Just a note, of course we could have done the node deletion manually and take care of the proper order, but that would have added some entity specific deletion code and using @PrivateOwned we can stick to a simple delete call in the Epos workspace, which keeps things simple when using the persistence API.

34 Comments

  1. Samuel L. says:

    After reading through the article, I just feel that I need more info. Could you suggest some resources ?

  2. If i have no ration I’m sorry, I was in other country and havent been here for a long time, but that note I was reading somewhere elseare you copycatplaces?

  3. Everything is very open with a very clear explanation of the challenges.
    It was definitely informative. Your site is very helpful.
    Thank you for sharing!

  4. I like it whenever people come together and share thoughts.
    Great website, continue the good work!

    Also visit my blog post; christmas decoration storage

  5. Howdy! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.
    Any recommendations?

    Review my site home warranty georgia :: http://www.gahomewarranties.com/
    ::

  6. Oh my goodness! Impressive article dude! Many thanks,
    However I am having troubles with your RSS. I don’t know the reason why I can’t subscribe to it.
    Is there anybody having similar RSS issues? Anyone who knows the answer can you kindly respond?
    Thanx!!

    My blog post: home security systems

  7. Hey Amy, this is the second time I’ve entered into one of your festivals. I’m looking forward to a weekend of FUN! Well, I’ve only made about 10 (?) quilts so far. I just wish I had more time to sew! LOL!

  8. I all the time used to study paragraph in news papers but now as I am a user of
    web thus from now I am using net for articles or reviews, thanks
    to web.

  9. I discovered your blog internet site on bing and appearance several of your early posts. Preserve up the very good operate. I just now additional the RSS feed to my MSN News Reader. Seeking toward reading far more on your part down the road!…

  10. kohlscareer says:

    We’re a group of volunteers and starting a brand new scheme
    in our community. Your website provided us with helpful information to work
    on. You have done an impressive job and our entire neighborhood will be thankful to you.

  11. Spot up with this writeup, I truly feel this fabulous website wants additional consideration. Ill likely to end up again to read much more, thank you for that info.

  12. For most up-to-date information you have to visit web
    and on world-wide-web I found this site as a most excellent site
    for hottest updates.

  13. Hello there, just became aware of your blog through Google, and found that it’s really informative.

    I am gonna watch out for brussels. I’ll be grateful if you continue this
    in future. Lots of people will be benefited from your writing.
    Cheers!

  14. Malorie says:

    Hi there this is kind of of off topic but I was wanting to know
    if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding know-how so I wanted to get guidance
    from someone with experience. Any help would be greatly appreciated!

  15. Thanks for sharing your info. I truly appreciate your efforts and I will be waiting
    for your next write ups thanks once again.

  16. Ahaa, its fastidious discussion regarding this piece of writing at this place at this website, I have read all that,
    so now me also commenting at this place.

    http://www.atomagicloud.com/michael-kors-outlet

  17. bit.ly says:

    Now I am ready to do my breakfast, when having my breakfast coming again to read
    further news.

  18. best adjustable dumbbells…

    Epos » Blog Archive » JPA, Eclipselink and storing hierarchies…

  19. Thanks in support of sharing such a pleasant idea, paragraph is nice, thats why i have read it completely

  20. buzzing in ears…

    Epos » Blog Archive » JPA, Eclipselink and storing hierarchies…

  21. Throughout this article I’m to show 7 things you need to know
    so that you can start making money online today.
    It is time to take control and make your own money online, too.
    Sell services (do you know how big a business being a Virtual Assistant is these days.

    Here is my webpage – profit maximiser scam

  22. Is just a leading supplier for a large number of goods,
    corporations, and solutions of impartial critiques and ranks.

    Your target, at, is to offer good and correct reviews which might be representative of the business in question.
    We’ll help you find another educator or refund your entire purchase
    if for any explanation you aren’t satisfied after your
    training. LVMPD sgt ofc DOMINGO LUCERO JR L WEBB, and ofc RYAN T.

    DEWEY broke my rights at the same time.

  23. You should take part in a contest for one of the best websites online.
    I am going to highly recommend this site!

  24. sorbetiere says:

    Everything is very open with a really clear explanation of
    the issues. It was truly informative. Your website is extremely helpful.

    Thank you for sharing!

  25. After looking at a few of the articles on your website, I really appreciate your technique of writing a
    blog. I bookmarked it to my bookmark site list and will be checking back soon. Take a look at my web site as well and let me know your
    opinion.

  26. frullatore says:

    Thank you a lot for sharing this with all of us
    you actually understand what you are talking about!
    Bookmarked. Kindly additionally seek advice from my website =).
    We may have a link change agreement between us

  27. I love what you guys are usually up too. This kind of clever work and reporting!
    Keep up the wonderful works guys I’ve added you guys
    to my personal blogroll.

  28. I pay a quick visit each day some websites and
    sites to read content, except this website gives feature based writing.

  29. It is really a great and useful piece of info.
    I am glad that you shared this useful info with us.
    Please stay us up to date like this. Thank you for sharing.

  30. Hello to every , because I am really keen of reading this website’s post to be
    updated on a regular basis. It includes nice data.

  31. It’s amazing designed for me to have a web site, which is valuable for my know-how.
    thanks admin

  32. naturally like your web-site but you need to check the spelling on several of your posts.
    Many of them are rife with spelling issues and I find it very troublesome to inform the reality nevertheless I will certainly come
    back again.

  33. gay cialis says:

    Great goods from you, man. I’ve be aware your stuff
    prior to and you’re simply extremely great.
    I actually like what you’ve obtained right here, really like
    what you are saying and the best way during which you are saying it.
    You are making it entertaining and you continue to care for to keep it sensible.
    I can’t wait to read much more from you. This is actually a wonderful web site.

  34. gay cialis says:

    Hi there, I want to subscribe for this website to take latest
    updates, therefore where can i do it please assist.

Leave a Reply