I am also using dom4j dynamic model. I have a problem with removing of an entity from a list of values.
Suppose, I have the following mapping:
Code:
<hibernate-mapping>
<class entity-name="Parent" node="Parent">
<id name="id_" type="long">
<generator class="native"/>
</id>
<list name="children">
<key column="parentid_"/>
<list-index column="LIST_INDEX"/>
<one-to-many class="Child"/>
</list>
</class>
<class entity-name="Child" node="Child">
<id name="id_" type="long">
<generator class="native"/>
</id>
<property name="name" type="string"/>
</class>
</hibernate-mapping>
Also, suppose, I have an instance of "Parent" with more than one "Child" in "children" list.
When I execute the following code:
Code:
Element parent = ....;
Element children = parent.element("children");
List childrenList = children.elements("Child");
for (int i = 0, k = childrenList.size(); i < k; i++) {
Element child = (Element)childrenList.get(i);
[b]children.remove(child);[/b]
session.delete(valueElement_);
}
session.update(parent);
hibernate updates the database, but child elements still contains in children element. In other words, when
children.remove(child); line is executed, it does not work because org.hibernate.tuple.ElementWrapper wrongly implements equals(Object) method (children is an instance of org.dom4j.tree.DefaultElement and child is an instance of org.hibernate.tuple.ElementWrapper).
Here is more simplified example of the bug:
Code:
DefaultElement child1 = new DefaultElement("child", null);
child1.setText("child 1");
DefaultElement child2 = new DefaultElement("child", null);
child2.setText("child 2");
ElementWrapper child1Wrapper = new ElementWrapper(child1);
ElementWrapper child2Wrapper = new ElementWrapper(child2);
DefaultElement parent = new DefaultElement("parent");
parent.add(child1Wrapper);
parent.add(child2Wrapper);
parent.remove(child1Wrapper);
parent.remove(child2Wrapper);
Iterator childs = parent.elementIterator();
while(childs.hasNext()) {
Element ch = (Element)childs.next();
System.out.println(ch.getText());
}