-->
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.  [ 13 posts ] 
Author Message
 Post subject: Composite key: column duplicated in 3 <key-many-to-one>
PostPosted: Wed Jan 14, 2004 11:23 am 
Newbie

Joined: Tue Jan 06, 2004 3:49 pm
Posts: 11
Location: Montreal, Canada
We have a composite key where the same column (ccid) is duplicated in three <many-to-one> elements:

Code:
<composite-id name="comp_id" class="aPackage.MtncpPK">
  <meta attribute="field-description">
     @hibernate.id
      generator-class="assigned"
  </meta>
 
  <key-property name="mode" column="mode" type="java.lang.String" length="4"/>
 
  <!-- bi-directional many-to-one association to Mtcep -->
  <key-many-to-one name="mtcepByCcidAndNcell"
                   class="aPackage.Mtcep">
    <column name="ccid"/>
    <column name="ncell" />
  </key-many-to-one>
 
  <!-- bi-directional many-to-one association to Mtcep -->
  <key-many-to-one name="mtcepByCcidAndCell"
                   class="aPackage.Mtcep">
    <column name="ccid"/>
    <column name="cell" />
  </key-many-to-one>
 
  <!-- bi-directional many-to-one association to SwitchView -->
  <key-many-to-one name="switchView"
                   class="aPackage.SwitchView">
    <column name="ccid"/>
  </key-many-to-one>
</composite-id>


Naturally this causes a mapping error (see below) since we have to identify the dominant property by setting the others with
Code:
update="false" insert="false"
.

Problem is, as per the DTD, the <key-many-to-one> element does not allow
Code:
update="false" insert="false"
. In that case, how exactly should we specify the dominant element?

Thought I had found a solution to my problem in this thread: http://forum.hibernate.org/viewtopic.php?t=926346, but there is no solution for a <key-many-to-one> element.

mapping error
--------------
Code:
Caused by: java.lang.RuntimeException: Exception building SessionFactory: Repeated column in mapping for class se.ericsson.lmc.ccl.model.dto.temp_lmcfrob_hbm.hibernate.MtocpExt should be mapped with insert="false" update="false": ccid
   at aPackage.(HibernateUtil.java:29)
   ... 37 more
Caused by: net.sf.hibernate.MappingException: Repeated column in mapping for class se.ericsson.lmc.ccl.model.dto.temp_lmcfrob_hbm.hibernate.Mtncp should be mapped with insert="false" update="false": ccid
   at net.sf.hibernate.persister.AbstractEntityPersister.checkColumnDuplication(AbstractEntityPersister.java:1000)
   at net.sf.hibernate.persister.EntityPersister.(EntityPersister.java:757)
   at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:41)
   at net.sf.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:137)
   at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:739)
   at (HibernateUtil.java:25)
   ... 37 more



Thanking you all in advance for the help!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 11:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://forum.hibernate.org/viewtopic.php?t=926211&highlight=keyproperty+insert+keymanytoone

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 11:42 am 
Newbie

Joined: Tue Jan 06, 2004 3:49 pm
Posts: 11
Location: Montreal, Canada
Shame on me for not finding that post :(

I just can't believe the support you guys are providing, FAST and accurate, two thumbs way up!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 12:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
:)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Still require some advice...
PostPosted: Wed Jan 14, 2004 2:54 pm 
Newbie

Joined: Tue Jan 06, 2004 3:49 pm
Posts: 11
Location: Montreal, Canada
Emmanuel,

We tried to implement what you recommend in the referenced topic http://forum.hibernate.org/viewtopic.php?t=926211&highlight=keyproperty+insert+keymanytoone. However we now receive the following exception:

Code:
Caused by: net.sf.hibernate.MappingException: identifier mapping has wrong number of columns: aPackage.Mtncp type: aPackage.MtncpPK
   at net.sf.hibernate.mapping.RootClass.validate(RootClass.java:201)
   at net.sf.hibernate.cfg.Configuration.validate(Configuration.java:576)
   at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:733)
   at aPackage..(HibernateUtil.java:25)


Our mapping file looks as follow:
Code:
<composite-id name="comp_id" class="aPackage.MtncpPK">
        <meta attribute="field-description">
           @hibernate.id
            generator-class="assigned"
        </meta>
       
        <key-property name="mode" column="mode" type="java.lang.String" length="4"/>
       
        <!-- bi-directional many-to-one association to Mtcep -->
        <key-property name="mtcepByCcidAndNcell"
                         type="aPackage.Mtcep">
           <column name="ccid"/>
           <column name="ncell"/>
         </key-property>
       
        <!-- bi-directional many-to-one association to Mtcep -->
        <key-property name="mtcepByCcidAndCell"
                          type="aPackage.Mtcep">
           <column name="ccid"/>
           <column name="cell"/>
         </key-property>
       
        <!-- bi-directional many-to-one association to SwitchView -->
        <key-property name="switchView"
                          type="aPackage.SwitchView">
           <column name="ccid"/>
         </key-property>
</composite-id>   

...

<!-- associations -->
    <many-to-one name="mtcepByCcidAndNcell"
                 update="false"
                 insert="false"
                 class="se.ericsson.lmc.ccl.model.dto.temp_lmcfrob_hbm.hibernate.Mtcep">
      <column name="ccid"/>
       <column name="ncell"/>
    </many-to-one>
   
    <many-to-one name="mtcepByCcidAndCell"
                 update="false"
                 insert="false"
                 class="se.ericsson.lmc.ccl.model.dto.temp_lmcfrob_hbm.hibernate.Mtcep">
      <column name="ccid"/>
       <column name="cell"/>
    </many-to-one>
   
    <many-to-one name="switchView"
                 class="se.ericsson.lmc.ccl.model.dto.temp_lmcfrob_hbm.hibernate.SwitchView">
      <column name="ccid"/>
    </many-to-one>


I'm afraid we're missing something quite obvious, but we can't seem to put our finger on the problem :(

Anyhow, pointers would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 4:54 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Actually, this is the same issue, you have to key-property tags for each column.
The real good solution is using surrogate keys.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 12:47 am 
Newbie

Joined: Tue Jan 06, 2004 3:49 pm
Posts: 11
Location: Montreal, Canada
Emmanuel:
Quote:
Try to replace key-many-to-one by a key-property and add 2 simple <many-to-one update="false" insert="false"> outside the composite-id

quoted from: http://forum.hibernate.org/viewtopic.php?t=92621


Emmanuel,

How then could we implement the workaround you recommended above? I'm still scratching my head over this one...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 7:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Something like that
Code:
<key-property name="mode" column="mode" type="java.lang.String" length="4"/>
       
        <!-- bi-directional many-to-one association to Mtcep -->
        <key-property name="ccid"
                         type="int"/>
        <key-property name="ncell"
                         type="int"/>
                </key-property>
</composite-id>
<many-to-one name="mtcepByCcidAndNcell"
                 update="false"
                 insert="false"
                 class="se.ericsson.lmc.ccl.model.dto.temp_lmcfrob_hbm.hibernate.Mtcep">
      <column name="ccid"/>
       <column name="ncell"/>
    </many-to-one>


I personnally would have add a generated surrogate key

_________________
Emmanuel


Top
 Profile  
 
 Post subject: similar issue in an EJB 3.0/JBoss context
PostPosted: Thu Dec 30, 2004 3:54 am 
Newbie

Joined: Sun Dec 26, 2004 4:39 pm
Posts: 14
Hello,

I'm getting the same type of an errot " Repeated column in mapping" for a class that has a composite primary key. One of the columns of the key is the joincolumn of a many-to-relationship.

I'm using JBoss 4.0.1 implementation of the EJB 3.0 spec and there is no way that I can find of setting a Many to One relationship as update/updatable=false, insert/insertable=false.

Not really a Hibernate issue but I thought someone might have a work around.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 30, 2004 5:27 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That's interesting, we probably should avoid that in the spec.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: not sure I understand the answer
PostPosted: Thu Dec 30, 2004 3:15 pm 
Newbie

Joined: Sun Dec 26, 2004 4:39 pm
Posts: 14
Emmanuel,

Thanks for the response but I'm not sure I understand your answer. What can I do now to get this working?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 03, 2005 10:35 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Either :
- don't use the annotations for this particular case, use hbm.xml instead
- use 2 columns
- use a surrogate key

I know these answers are not really satisfactory

_________________
Emmanuel


Top
 Profile  
 
 Post subject: I'm also facing the isuue like that
PostPosted: Thu Dec 06, 2007 3:18 am 
Newbie

Joined: Mon Nov 19, 2007 3:15 am
Posts: 2
Hi,
I'm also facing the issue like that.

Tables created in MySQL:

CREATE TABLE PARTY (
PARTY_ID INTEGER,
PARTY_TYPE VARCHAR(60) NOT NULL,
CONSTRAINT XPKPARTY PRIMARY KEY (PARTY_ID)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE ROLE (
ROLE_ID INTEGER,
ROLE_TYPE VARCHAR(60) NOT NULL,
CONSTRAINT XPKROLE PRIMARY KEY (ROLE_ID)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE PARTY_ROLE (
ROLE_ID INTEGER,
PARTY_ID INTEGER,
CONSTRAINT XPKPARTY_ROLE PRIMARY KEY (ROLE_ID, PARTY_ID),
CONSTRAINT FK_PARTY FOREIGN KEY (PARTY_ID) REFERENCES PARTY (PARTY_ID),
CONSTRAINT FK_ROLE FOREIGN KEY (ROLE_ID) REFERENCES ROLE (ROLE_ID)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE RELATIONSHIP_RESULT (
ROLE_ID_FROM INTEGER,
ROLE_ID_TO INTEGER,
RELATIONSHIP_RESULT VARCHAR(60) NOT NULL,
CONSTRAINT XPKRELATIONSHIP_RESULT PRIMARY KEY (ROLE_ID_FROM, ROLE_ID_TO)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE RELATIONSHIP (
MEMBERSHIP_ID INTEGER,
RL_PARTY_ID_FROM INTEGER NOT NULL,
RL_ROLE_ID_FROM INTEGER NOT NULL,
RL_PARTY_ID_TO INTEGER NOT NULL,
RL_ROLE_ID_TO INTEGER NOT NULL,
CONSTRAINT XPKRELATIONSHIP PRIMARY KEY (MEMBERSHIP_ID),
CONSTRAINT FK_ROLE_FROM FOREIGN KEY (RL_ROLE_ID_FROM, RL_PARTY_ID_FROM) REFERENCES PARTY_ROLE (ROLE_ID, PARTY_ID),
CONSTRAINT FK_ROLE_TO FOREIGN KEY (RL_ROLE_ID_TO, RL_PARTY_ID_TO) REFERENCES PARTY_ROLE (ROLE_ID, PARTY_ID),
CONSTRAINT FK_RELN_RESULT FOREIGN KEY (RL_ROLE_ID_FROM, RL_ROLE_ID_TO) REFERENCES RELATIONSHIP_RESULT (ROLE_ID_FROM, ROLE_ID_TO)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

Using Hibernate tools to generate the hbm and pojo files.

In Relationship.hbm.xml shows:

<class name="com.cts.dao.pom.Relationship" table="relationship" catalog="test">
<comment></comment>
<id name="membershipId" type="int">
<column name="MEMBERSHIP_ID" />
<generator class="assigned" />
</id>
<many-to-one name="partyRoleByFkRoleTo" class="com.cts.dao.pom.PartyRole" fetch="select">
<column name="ROLE_ID_TO" not-null="true">
<comment></comment>
</column>
<column name="PARTY_ID_TO" not-null="true">
<comment></comment>
</column>
</many-to-one>
<many-to-one name="relationshipResult" class="com.cts.dao.pom.RelationshipResult" update="false" insert="false" fetch="select">
<column name="ROLE_ID_FROM" not-null="true">
<comment></comment>
</column>
<column name="ROLE_ID_TO" not-null="true">
<comment></comment>
</column>
</many-to-one>
<many-to-one name="partyRoleByFkRoleFrom" class="com.cts.dao.pom.PartyRole" update="false" insert="false" fetch="select">
<column name="ROLE_ID_FROM" not-null="true">
<comment></comment>
</column>
<column name="PARTY_ID_FROM" not-null="true">
<comment></comment>
</column>
</many-to-one>
</class>

In the <many-to-one name="partyRoleByFkRoleTo" tag there is no attributes having insert="false" update ="false"

but in the same table the other reference for <many-to-one name="partyRoleByFkRoleFrom tag there it having the attibutes insert="false" update ="false".

We can able to insert partyRoleByFkRoleTo object in to Relationship table but cannot able to insert partyRoleByFkRoleFrom object in to Relationship table. How we insert the both the object in to the Relationship table?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.