Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 537983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:59:03+00:00 2026-05-13T09:59:03+00:00

Need some help on understanding how to do this; I’m going to be running

  • 0

Need some help on understanding how to do this; I’m going to be running recursive ‘find’ on a file system and I want to keep the information in a single DB table – with a self-referencing hierarchial structure:

This is my DB Table structure I want to populate.

DirObject Table:

id       int NOT NULL,
name     varchar(255) NOT NULL,
parentid int NOT NULL);

Here is the proposed Java Class I want to map (Fields only shown):

public DirObject {
    int id;
    String name;
    DirObject parent;
...

For the ‘root’ directory was going to use parentid=0; real ids will start at 1, and ideally I want hibernate to autogenerate the ids.

Can somebody provide a suggested mapping file for this please; as a secondary question I thought about doing the Java Class like this instead:

public DirObject {
    int id;
    String name;
    List<DirObject> subdirs;

Could I use the same data model for either of these two methods ? (With a different mapping file of course).

— UPDATE: so I tried the mapping file suggested below (thanks!), repeated here for reference:

<hibernate-mapping>
    <class name="my.proj.DirObject" table="category">
        ...   

        <set name="subDirs" lazy="true" inverse="true">
            <key column="parentId"/>
            <one-to-many class="my.proj.DirObject"/>
        </set>

        <many-to-one name="parent"
                     class="my.proj.DirObject"
                     column="parentId" cascade="all" />
    </class>

…and altered my Java class to have BOTH ‘parentid’ and ‘getSubDirs’ [returning a ‘HashSet’].

This appears to work – thanks, but this is the test code I used to drive this – I think I’m not doing something right here, because I thought Hibernate would take care of saving the subordinate objects in the Set without me having to do this explicitly ?

DirObject dirobject=new DirObject();
   dirobject.setName("/files");
   dirobject.setParent(dirobject);

   DirObject d1, d2;
   d1=new DirObject(); d1.setName("subdir1"); d1.setParent(dirobject);
   d2=new DirObject(); d2.setName("subdir2"); d2.setParent(dirobject);
   HashSet<DirObject> subdirs=new HashSet<DirObject>();
   subdirs.add(d1);
   subdirs.add(d2);
   dirobject.setSubdirs(subdirs);


   session.save(dirobject);
   session.save(d1);
   session.save(d2);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:59:04+00:00Added an answer on May 13, 2026 at 9:59 am

    you can get the children from parent

    <set name="subdirs" lazy="false" cascade="all-delete-orphan" inverse="true">
                <key column="parentid " />
                <one-to-many class="DirObject" />
     </set>
    

    parent from child

    <many-to-one name="parent" class="DirObject">
                <column name="parentid" />
     </many-to-one>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.