-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Mapping class & subclass to same table without discrimin
PostPosted: Thu Mar 23, 2006 12:38 pm 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
(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>


_________________
-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 2:39 pm 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
I modified my mapping and it fixed the problem.

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


To:
Code:
<bag
            name="customFields"
            lazy="true"
            inverse="false"
            cascade="all-delete-orphan"
        >
            <key>
               <column
                  name="itemId"
                  not-null="true"
               >
               </column>
            </key>

            <one-to-many
                class="test.CustomField"
            />
      </bag>



The difference being the use of the column element as opposed to the column & not-null attributes in <key>.

_________________
-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 10:47 am 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
Ok I may have gotten too excited when I saw the different mapping which looked like it would work the same. What i found, is that using the <column name="..." not-null="true"/> element DOES NOT seem to work correctly. It seems to ignore the not-null, since it tries inserting WITHOUT the foreign key.

_________________
-Chris


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.