java - Hibernate3: Self-Referencing Objects -
Need some help in understanding how to do this; I'm running a recursive 'find' on the file system and I want to put the information in a DB table - with a self-referenced hierarchical structure:
This is my DB table structure.
DirObject table:
is not id int, name varchar (255) no, grandparent, parent not int);
This is the proposed Java class that I want to map (fields shown only):
public derobbet {int id; String name; Direct criteria; ...
was going to use parent = 0 for the 'root' directory; Actual ID will start with 1, and ideally I need hibernate to automatically create an ID.
Does anyone please provide a suggested mapping file for this? As a secondary question, I thought about doing Java class instead:
public debarbox {int id; String name; & Lt; DirObject & gt; Subdirectory;
Can I use the same data model for either of these two methods? (With a different mapping file of course).
--- Update: So I suggested the mapping file (thanks!), Repeated here for reference:
& lt; Hibernate-Mapping & gt; & Lt; Class name = "my.proj.DirObject" table = "category" & gt; ... & lt; Set name = "subdir" lazy = "true" inverse = "true" & gt; & Lt; Key column = "parentId" /> & Lt; One-to-many classes = "my.proj.DirObject" /> & Lt; / Set & gt; & Lt; Many-to-one names = "parent" class = "my.proj.DirObject" column = "parentId" cascade = "all" /> & Lt; / Square & gt;
... and changed my Java class to both 'parent' and 'gatebirds' [to return a 'hashaseet'].
It seems to work - thank you, but this is the test code that I used to run it - I think I am not doing anything here because I thought that Should be careful to save the subordinated object in the set without hibernation, without having to do it clearly?
DirObject dirobject = new DirObject (); Dirobject.setName ("/ files"); Dirobject.setParent (dirobject); DirObject D1, D2; D1 = new direct (); D1.setName ("subdir1"); D1.setParent (dirobject); D2 = new direct (); D2.setName ("subdir2"); D2.setParent (dirobject); HashSet & LT; DirObject & gt; Subdirs = new hashset & lt; DirObject & gt; (); Subdirs.add (d1); Subdirs.add (d2); Dirobject.setSubdirs (subdirectory); Session.save (dirobject); Session.save (d1); Session.save (d2);
You can get children from parents
& lt; Set name = "subdirs" lazy = "false" cascade = "all-delete-orphan" inverted = "true" & gt; & Lt; Major column = "parent" /> & Lt; One-to-many classes = "diebugs" /> & Lt; / Set & gt;
Parents to the child
& Lt; / Many-to-one & gt;
Comments
Post a Comment