Database-operations on Recursive data using hibernate

Source: Internet
Author: User

During work, we often encounter data with recursive records. The most common is the data of a department node of an organization. A node must have a parent node attribute, at the same time, it may have several subnodes. All node data is stored in a table in the database. This phenomenon is called the Composite mode in the design mode. Next I will give an example of using hibernate to operate such a table. For the hibernate configuration and table structure creation section, please refer to my previous article 《Example of using the weblogic data source as the hibernate Data Source"

1. Persistence class Node. java

  1. PackageCom. jarie. business. organization;
  2. /**
  3. *

    Title:

  4. *

    Description: department node

  5. *

    Copyright: Copyright (c) 2003

  6. *

    Company: www.jagie.com

  7. * @ Author Jagie
  8. * @ Version 1.0
  9. */
  10. Public ClassNode {
  11. Private StringID;// Pk
  12. Private StringName;// Name
  13. Private StringDescription;// Description
  14. PrivateNode parent;// Superior department
  15. PrivateJava. util.SetChildren;// Subordinate departments
  16. Public Static VoidMain (String[] Args ){
  17. }
  18. Public StringGetID (){
  19. ReturnID;
  20. }
  21. Public VoidSetID (StringID ){
  22. This. ID = ID;
  23. }
  24. Public StringGetName (){
  25. ReturnName;
  26. }
  27. Public VoidSetName (StringName ){
  28. This. Name = name;
  29. }
  30. PublicNode getParent (){
  31. ReturnParent;
  32. }
  33. Public VoidSetParent (Node parent ){
  34. This. Parent = parent;
  35. }
  36. Public StringGetDescription (){
  37. ReturnDescription;
  38. }
  39. Public VoidSetDescription (StringDescription ){
  40. This. Description = description;
  41. }
  42. Public StringToString (){
  43. ReturnName;
  44. }
  45. PublicJava. util.SetGetChildren (){
  46. ReturnChildren;
  47. }
  48. Public VoidSetChildren (java. util.SetChildren ){
  49. This. Children = children;
  50. }
  51. }


2. Map File Node. hbm. xml
 


  
    
      
    
    
    
    
      
      
    
           column="parent" name="parent" />
  


3. Use SchemaExport to create a database (omitted)

4. Test code snippet

  1.  // Obtain net. sf. hibernate. Session and net. sf. hibernate. Transaction;
  2. Session s = JDOUtils. getInstance (). getSessionFactory (). openSession ();
  3. Transaction t = s. beginTransaction ();
  4.  // Create a parent node
  5. Node org =NewNode ();
  6. Org. setName ("Beijing Head Office ");
  7. Org. setDescription ("this is Beijing Head Office ");
  8.  // Create a subnode
  9. Node org2 =NewNode ();
  10. Org2.setName ("Haidian branch ");
  11. Org2.setDescription ("this is the Haidian branch ");
  12.  // Create a subnode reference to the parent node
  13. Org2.setParent (org );
  14. &

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.