(First, I'd like to Note that the only reason for this is to support legacy schema/code in our app)
I have class A, which is a hibernate entity with generated primary keys. However, there is one case where the primary key actually needs to be assigned. For this, I create class B (which extends A), and override the primary key getter/setter, and map it as an "assigned" pk.
Basically, the mapping document for B is identical to A, except for this primary key generator.
So far this is OK. The reason this works, is because I never have to retrieve a B. I only need to save a B, and later it will always be viewed as an A.
The Problem:
I have a one-to-many collection mapped as a bag, with the collection key set as not-null="true". This is necessary, since this ensures that the FK will always be set on an Insert.
Since both A and B have this mapping, and B is actually an A, somewhere under the hood 2 identical "Backrefs" are created, causing an exception on hibernate initialization:
Code:
org.hibernate.MappingException: duplicate property mapping: _customFieldsBackref at org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:389) at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:379) at org.hibernate.mapping.RootClass.validate(RootClass.java:192) at org.hibernate.cfg.Configuration.validate(Configuration.java:983) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1147) at
I traced through the hibernate source, and found that using not-null="true" for the collection key, creates these "Backref" pointers. Since I have B extending A, it thinks B is actually an A, and creates 2 identical Backref pointers.
Any suggestions for a different way to handle this?
Hibernate version: 3.1
Mapping documents: Both A.hbm.xml and B.hbm.xml have this mapping for the one-to-many collection:
Code:
<bag
name="customFields"
lazy="true"
inverse="false"
cascade="all-delete-orphan"
>
<key
column="itemId"
not-null="true"
>
</key>
<one-to-many
class="test.CustomField"
/>
</bag>