Mapping a collection of entities from two different classes with the same collection name results in duplicate backref property exception if collection keys are not null

Description

Mapping a child entity collection from two different parent entities results in duplicate backref property exception when configuring the session factory if the collection keys are marked not null and the collection names are the same in both parents.

Log is:
(cfg.Environment 509 ) Hibernate 3.2.3
(cfg.Environment 542 ) hibernate.properties not found
(cfg.Environment 676 ) Bytecode provider name : cglib
(cfg.Environment 593 ) using JDK 1.4 java.sql.Timestamp handling
(cfg.Configuration 1426) configuring from resource: /hibernate.cfg.xml
(cfg.Configuration 1403) Configuration resource: /hibernate.cfg.xml
(cfg.Configuration 553 ) Reading mappings from resource : com/bbsw/tests/ParentA.hbm.xml
(cfg.HbmBinder 300 ) Mapping class: com.bbsw.tests.ParentA -> PARENT_A
(cfg.Configuration 553 ) Reading mappings from resource : com/bbsw/tests/ParentB.hbm.xml
(cfg.HbmBinder 300 ) Mapping class: com.bbsw.tests.ParentB -> PARENT_B
(cfg.Configuration 553 ) Reading mappings from resource : com/bbsw/tests/Child.hbm.xml
(cfg.HbmBinder 300 ) Mapping class: com.bbsw.tests.Child -> CHILD
(cfg.Configuration 1541) Configured SessionFactory: null
(cfg.HbmBinder 2375) Mapping collection: com.bbsw.tests.ParentA.children -> CHILD
(cfg.HbmBinder 2375) Mapping collection: com.bbsw.tests.ParentB.children -> CHILD
Exception in thread "main" org.hibernate.MappingException: Duplicate property mapping of _childrenBackref found in com.bbsw.tests.Child
at org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:459)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:449)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
at HBTest.main(HBTest.java:17)

The problem seems to be originated during the second Collection binding pass in org.hibernate.cfg.HbmBinder, as the Backrefs and IndexBackrefs names are created like this:

(Excerpts from HbmBinder.java r10921 2006-12-05 14:39:12Z steve.ebersole@jboss.com, contained in the Hibernate 3.2.3.ga source)

line 2242: IndexBackref ib = new IndexBackref();
line 2243: ib.setName( '_' + node.attributeValue( "name" ) + "IndexBackref" );

and

line 2478: Backref prop = new Backref();
line 2479: prop.setName( '_' + node.attributeValue( "name" ) + "Backref" );

in both cases the node name value is the collection name. So for every collection that points to a child, Hibernate creates a backref property without considering the posibility of more than one entity declaring the same collection name.

I've patched the code to add the collection owner's entity name to the property name, and it worked without problems.

I'm attaching:

  • ParentA, ParentB and Child classes

  • Mappings for ParentA, ParentB and Child

  • hbm.xml file

  • Main test case. (HBTest.java)

  • Proposed patch in unified diff format.

Environment

Proven on Hibernate 3.2.2 and 3.2.3, PostgreSQL 8.2.3, WindowsXP, Java 1.5

Activity

Show:
Miguel Griffa
July 9, 2007, 6:52 PM

I have been hitted by this bug and having found this, I haven't found more info about this. I'd really like to use a release (or cvs) version of hibernate rather than a personally patched one. are there any ideas or discussions about this?
any known workaround?

Swen Lanthemann
July 11, 2007, 1:30 PM

I've been hitted by this bug too. I tried something like a workaround and it seems to work.

Changes in the attached Hibernate-bug.zip file.
In the Child.hbm.xml File i've mapped the Child class more than once with the entity-name="test"
<class
name="com.bbsw.tests.Child"
table="CHILD"
entity-name="test"
schema="hibernate_tests"
polymorphism="explicit"
dynamic-update="true"
mutable="true"
>
...

In the ParentA.hbm.xml i've changed the one-to-many relationship in the list children and set instead of the class, the entity-name="test".
...
<list name="children" lazy="true" inverse="false"
cascade="persist,all-delete-orphan">
<key not-null="true" update="false">
<column name="PARENTA_ID" not-null="true" />
</key>
<list-index column="PARENTA_LISTINDEX">
</list-index>
<one-to-many entity-name="test" />
</list>
...

After this changes the duplicate property exception disappears.

Diego Plentz
October 5, 2007, 2:58 PM

Fixed in trunk/3.2

thanks

Steve Ebersole
March 21, 2011, 7:09 PM

Bulk closing stale resolved issues

Assignee

Diego Plentz

Reporter

GuidoG

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure