[hibernate-issues] [Hibernate-JIRA] Created: (HHH-3211) The method org.hibernate.tuple.ElementWrapper.equals(Object) is implemented incorrectly

Sergey (JIRA) noreply at atlassian.com
Mon Mar 31 10:54:33 EDT 2008


The method org.hibernate.tuple.ElementWrapper.equals(Object) is implemented incorrectly
---------------------------------------------------------------------------------------

                 Key: HHH-3211
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3211
             Project: Hibernate3
          Issue Type: Bug
          Components: core
         Environment: Hibernate 3.2.5, MySQL 5.0 (version/database does not matter for this bug)
            Reporter: Sergey


1. Suppose, we have the following mapping (DOM4J entity mode):

<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> 

2. Suppose, we have an instance of "Parent" with more than one "Child" in "children" list.

Hibernate makes correct changes into database, but does not remove "Child" elements from "Parent" when the following code executed:

      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);
         children.remove(child);  // here child element should be removed from children element but can not be removed
         session.delete(child);
      }
      session.update(parent); 


This bug is reproduced only for two or more number of entities in hibernate collection (because of org.dom4j.tree.DefaultElement implementation peculiarity). This situation occurs because the method ElementWrapper.equals(Object) returns FALSE when compares an instance of ElementWrapper with itself. As a result, it is impossible to remove an instance of ElementWrapper from any Collection/List.

Here is a simplified but full example of the bug:

      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());
      } 


To fix this bug, ElementWrapper should implement equals(Object) method in the way like the following:

	public boolean equals(Object other) {
		if (this == other)
			return true;
		if (other instanceof ElementWrapper) {
			ElementWrapper otherWraper = (ElementWrapper)other;
			Element otherElement = otherWraper.getElement();
			return element.equals(otherElement);
		}
		return element.equals(other);
	}



-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list