Hi,
I needed to create mapping for graph structure. It looks like normal tree with some exception. Every node has 0-N children and every node has 0-N parents (instead of only one). Node's children should be ordered, parents not.
I can do mapping for normal tree but I'm totally lost with this.
I read reference documentation and HIA, but can't make it work.
The "most" working solution, with which I've ended is attached at the end of this post. It makes at leat correct tables and when building the tree, it fills something inside. It's not throwing any exception. But when loading the data back, the structure is not correct. It seems all nodes are present in two copies. All examples I found were describing normal tree structure or many-to-many association with two different classes. But I need many-to-many, bidirectional, ordered (from one side) on the same class.
I would be glad for any hint or help.
<hibernate-mapping>
<class name="PageData" table="PageData">
<id name="id" column="pagedata_id" type="long">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<list
name="children"
table="children_parent"
cascade="save-update">
<key column="child_pagedata_id" />
<index column="position" />
<many-to-many class="PageData" column="pagedata_id"/>
</list>
<set
name="parents"
inverse="true"
table="children_parent">
<key column="child_pagedata_id" />
<many-to-many class="PageData" column="pagedata_id"/>
</set>
</class>
</hibernate-mapping>
-----------------------------------
public class PageData implements Serializable {
private Long id;
private String name;
private List<PageData> children = new ArrayList<PageData>();
private Set<PageData> parents = new HashSet<PageData>();
...
}
|