-->
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: Repeated column in mapping for collection, composite-map-key
PostPosted: Mon Apr 17, 2006 12:05 pm 
Newbie

Joined: Tue Jul 05, 2005 3:21 pm
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I have a one-to-many between two legacy DB tables.
The two tables have the same column name "CREDIT_REF_NUM"
It is he primary key in CREDIT_APP table (one) and part of composite key in associated table DOC (many).

CREDIT_APP table
CREDIT_REF_NUM - PK

DOC table
CREDIT_REF_NUM - PK, FK
FORM_ID - PK

I'm getting "org.hibernate.MappingException: Repeated column in mapping for collection: com.domain.application.Application.documents column: CREDIT_REF_NUM
"
Tha stack trace is bellow also.
I guess the use of formula='CREDIT_REF_NUM' would work but composite-map-key does not allow it.


Hibernate version:
3.1.2
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
package="com.domain.application">
<class
name="Application"
table="CREDIT_APP"
lazy="true"
abstract="false">
<id
name="referenceNumber"
type="com.persistence.hibernate.usertype.ReferenceNumberUserType"
column="CREDIT_REF_NUM">
<generator class="assigned" />
</id>
<timestamp
name="updateTimestamp"
unsaved-value="null"
column="TSTAMP">
</timestamp>

<map
name="documents"
cascade="all-delete-orphan"
lazy="true"
table="DOC"
inverse="true">
<key not-null="true">
<column
name="CREDIT_REF_NUM"
not-null="true">
</column>
</key>
<composite-map-key class="com.domain.application.DocumentKey">
<key-property
name="referenceNumber"
column="CREDIT_REF_NUM"
type="com.persistence.hibernate.usertype.ReferenceNumberUserType">
</key-property>
<key-property
name="formId"
column="FORM_ID"
type="com.persistence.hibernate.usertype.StringToCharUserType">
</key-property>
</composite-map-key>

<one-to-many
class="com.domain.application.DocumentImpl" />
</map>


</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
package="com.domain.application">
<class
name="DocumentImpl"
table="DOC"
abstract="false"
lazy="true">
<composite-id
name="DocumentKey"
class="com.domain.application.DocumentKey">
<key-property
name="referenceNumber"
type="com.persistence.hibernate.usertype.CreditReferenceNumberUserType"
column="CREDIT_REF_NUM"/>
<key-property
name="formId"
type="com.persistence.hibernate.usertype.StringToCharUserType"
column="FORM_ID"/>
</composite-id>


<timestamp
name="updateTimestamp"
unsaved-value="null"
column="PRINTED_TSTAMP">
</timestamp>
<property
name="updateUserID"
type="com.persistence.hibernate.usertype.StringToCharUserType"
column="PRINTED_BY_USER_ID"
not-null="true">
</property>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
org.hibernate.MappingException: Repeated column in mapping for collection: com.domain.application.Application.documents column: CREDIT_REF_NUM
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:290)
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:303)
at org.hibernate.mapping.Collection.validate(Collection.java:270)
at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:67)
at org.hibernate.cfg.Configuration.validate(Configuration.java:988)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1169)

Name and version of the database you are using:
DB2 8.1.0

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


    Last edited by idunare on Mon May 01, 2006 9:46 am, edited 5 times in total.

    Top
     Profile  
     
     Post subject:
    PostPosted: Tue Apr 18, 2006 7:49 pm 
    Expert
    Expert

    Joined: Thu Dec 23, 2004 9:08 pm
    Posts: 2008
    If CREDIT_REF_NUM is the key of the map, why are you trying to use it as the map-key, too? Change your mapping to have a non-composite map-key, with just FORM_ID.

    The key is used to determine ownership: all items in the DOC table with a give CREDIT_REF_NUM are owned by the CREDIT_APP with the same CREDIT_REF_NUM. The map-key is used to fins a specific DOC within that map: that's the FORM_ID.

    Can I suggest that you use code tags when posting code to the boards? It's hard to read mappings as left-aligned proportionally-spaced text.


    Top
     Profile  
     
     Post subject:
    PostPosted: Thu Apr 20, 2006 12:20 pm 
    Newbie

    Joined: Tue Jul 05, 2005 3:21 pm
    Posts: 6
    My initial implementation was (and still is) exactly as you describe it. With the FORM_ID as the map-key. This is the right way.

    However there were some suggestions that it could / should be done by reusing the AppDocumentKey class as the map key.
    After my initial posting I realized that the only way to do that is to have:


    Code:
    <map
             name="documents"
             cascade="all-delete-orphan"
             lazy="true"
             table="BCLK_DOC"
             inverse="true">
             <key not-null="true">
                <column
                   name="CREDIT_REF_NUM"
                   not-null="true">
                </column>
             </key>
             <map-key type="com.persistence.hibernate.usertype.AppDocumentKeyUserType">
                <formula>CREDIT_REF_NUM</formula>
                <column name="FORM_ID"></column>
             </map-key>
             <one-to-many
                class="com.domain.application.DocumentImpl" />
          </map>

    Although this works is not the preferred way to do it.
    Your response was very helpful.
    Thanks


    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.