This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Blog around the Campfire and the fly likes Simple Hibernate Tree Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Other » Blog around the Campfire
Bookmark "Simple Hibernate Tree Example" Watch "Simple Hibernate Tree Example" New topic
Author

Simple Hibernate Tree Example

Christopher Rygaard
Ranch Hand

Joined: Feb 22, 2014
Posts: 42
    
    3
Tree Example

This is the simplest way to illustrate a tree structure in Hibernate that I could cook up.

This example has only one persistent class: TreeNode. Notice that the annotations on TreeNode._children and TreeNode._parent show the semantics of the tree.

Then notice that this is an ordinary container/contained relationship. There really is nothing special about building a persistent tree.

This example relies on the semantics of eager loading. See my eager loading example and my lazy loading example.

This example also relies on the semantics of cascade saving. See my cascade save example.

Note: The original version of this post had a time-bomb in it. It would work as originally presented, but it could cause bizarre behavior if the pattern were used in a larger project. The original had an improper use of a java.util.List on line 11 of TreeNode.java (now it uses a java.util.Set). The issue is fully explained in my simple Hibernate list example.

Notice: This is provided with no warranty whatsoever. Use at your own risk.

This is the node class:

This is an example of how to use the persistent tree:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Simple Hibernate Tree Example
 
Similar Threads
Simple Hibernate Container Example
Simple Hibernate Eager Loading Example
Simple Hibernate Example with Inner Classes
Simple Hibernate Example with Final Classes and Final Methods
Simple Hibernate Cascade Save Example