I need a parent child relationship (with inverse="true"..) implemented with a list; reading documentation and posts in the forum I know that:
1) list is not the canonical form of collection to map parent/child
2) someone has proposed a workaround:
http://www.hibernate.org/193.html , but It seems that there are still some problems.
Starting from this, a (very) little modification:
PARENT CLASS:
public class Parent {
Long id;
Integer version;
String desc;
List childs = new ArrayList();
public void addChild(Child child) {
child.setParent(this);
childs.add(child);
}
public void delChild(int index) {
childs.remove(index);
}
// accessors and mutators omitted
}
CHILD CLASS:
public class Child {
Long id;
Integer version;
String description;
Parent parent;
public Integer getIndex() {
if (parent!=null) // for childs created but not yet added to parent...
return new Integer(this.getParent().getChilds().indexOf(this));
else
return null;
}
public void setIndex(Integer index) {
// do nothing
}
// other accessors and mutators omitted
}
PARENT MAPPING:
<class name="Parent">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<version name="version"/>
<property name="description" type="string"/>
<list name="childs" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="parent"/>
<index column="index"/>
<one-to-many class="Child"/>
</list>
</class>
CHILD MAPPING:
<class name="Child">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<version name="version"/>
<property name="description" type="string"/>
<property name="index" type="integer"/>
<many-to-one
name="parent"
column="parent"
class="Parent"
not-null="true"/>
</class>
It seems to work well, but any feedback will be appreciated.
Thanks