-->
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.  [ 11 posts ] 
Author Message
 Post subject: Many to Many with composite keys
PostPosted: Tue Dec 09, 2003 8:51 am 
Newbie

Joined: Tue Dec 09, 2003 8:31 am
Posts: 17
My company is converting our application to java, so we are working with an existing database. I have used middlegen to generate the hbm.xml files and hbm2java to generate the java files. I am trying to run a simple query but keep getting a MappingException. I noticed other posts where the poster had a similar issue but none of those posts were helpful in solving my issue. So here it is.

1 - Table A and table B have a many to many relationship resolved by C.
2 - Table B has characteristic table hanging off it called D, 1 to many.
3 - Table C has a composite primary key: A-key + B-key.
4 - Table D has a composite primary key: B-key + seqnum.
5 - Table C and D have a many to many relationship resolved by F.

In the above example, table F is the problem. Since the many to many is between the table D and table C the composite key ends up being:
A-key + B-key + B-Key + seqnum, where B-key obviously gets collapsed into one column.

The mapping exception is "net.sf.hibernate.MappingException: Repeated column in mapping for class com.invisioninc.data.hib.DtDaypartGroupMember should be mapped with insert="false" update="false": OUTLET_CD
at net.sf.hibernate.persister.AbstractEntityPersister.checkColumnDuplication(AbstractEntityPersister.java:970)"

In the simplified example table F equates to DtDaypartGroupMember and OUTLET_CD equates to B-key.

Here is the hbm:
<class
name="com.invisioninc.data.hib.DtDaypartGroupMember"
table="DT_DAYPART_GROUP_MEMBER"
>

<composite-id name="comp_id" class="com.invisioninc.data.hib.DtDaypartGroupMemberPK">
<!-- bi-directional many-to-one association to DtOutletDaypart -->
<key-many-to-one
name="dtOutletDaypart"
class="com.invisioninc.data.hib.DtOutletDaypart"
>
<column name="DAYPART_ID" />
<column name="OUTLET_CD" />
</key-many-to-one>
<!-- bi-directional many-to-one association to DtDaypartGroup -->
<key-many-to-one
name="dtDaypartGroup"
class="com.invisioninc.data.hib.DtDaypartGroup"
>
<column name="OUTLET_CD" />
<column name="DAYPART_GROUP_NUM" />
</key-many-to-one>
</composite-id>

<property
name="daysAndTimes"
type="java.lang.String"
column="DAYS_AND_TIMES"
length="100"
/>

<!-- associations -->

</class>

Thanks in advane for any help.

Craig


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 2:48 pm 
Newbie

Joined: Tue Dec 09, 2003 8:31 am
Posts: 17
bump

Anyone got a suggestion on how to deal with this in Hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 5:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
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

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Worked for me
PostPosted: Tue Jan 13, 2004 4:01 pm 
Newbie

Joined: Fri Nov 14, 2003 11:15 am
Posts: 4
Thank you Emmanuel. I have the following situation.

Table A is parent of table B in an identifying relationship creating a composite key
Table A is also parent of table C in an identifying relationship again creating a composite key
Table B and C have a many to many relationship resolved in table D.

Middlegen generated table D with 2 key-many-to-one tags, repeating the key of Table A in both. I got the "Repeated column in mapping" execption, which makes sense.

Using Emmanuel's clue, I replaced these 2 key-many-to-one tags with key-property tags for each column in Table D's composite key and then created 2 many-to-one associations with update and insert false. Seems to have worked.

Is there a plan to allow for update and insert attributes in the key-many-to-one tag? Does that even make sense, or is it a database design issue? Or is this a middlegen issue?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 7:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
id has to be inserted into db, so key-* cannot support insert/update I think.

Add this to the middlegen JIRA, but this is not a common case.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 8:37 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
I've raised a similar issue before: http://forum.hibernate.org/viewtopic.php?t=925446


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 1:32 pm 
Newbie

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

would you be so kind to post your modified .hbm.xml file? I'm having the same problem but I can't seem to get it right... Guess I'm missing something very obvious when trying to implement Emmanuel's tip.

Thanks


Top
 Profile  
 
 Post subject: Sure, Sorry I didn't see this sooner
PostPosted: Thu Jan 22, 2004 11:09 am 
Newbie

Joined: Fri Nov 14, 2003 11:15 am
Posts: 4
I changed table names and field name from file to match the TableA, TableB example I gave above. I didn't run them, so hopefully I got it right. Should give the idea at least. Changes between the Middlegen file and the one that ran are in red.

The mapping file generated by Middlegen:

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

<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="com.mjwall.TableD"
table="TABLE_D"
>
<meta attribute="field-description">
@hibernate.class
table="TABLE_D"
</meta>

<composite-id name="comp_id" class="com.mjwall.TableDPK">
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
</meta>
<!-- bi-directional many-to-one association to TableB -->
<key-many-to-one
name="tableB"
class="com.mjwall.TableB"
>
<column name="ID_B" />
<column name="ID_A" />
</key-many-to-one>
<!-- bi-directional many-to-one association to TableC -->
<key-many-to-one
name="tableC"
class="com.mjwall.TableC"
>
<column name="ID_C" />
<column name="ID_A" />
</key-many-to-one>

</composite-id>


<!-- associations -->

</class>
</hibernate-mapping>

And the mapping file after my edits.

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

<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="com.mjwall.TableD"
table="TABLE_D"
>
<meta attribute="field-description">
@hibernate.class
table="TABLE_D"
</meta>

<composite-id name="comp_id" class="com.mjwall.TableDPK">
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
</meta>
<key-property
name="IdA"
column="ID_A"
type="java.math.BigDecimal"
length="22"
/>
<key-property
name="IdB"
column="ID_B"
type="java.math.BigDecimal"
length="22"
/>
<key-property
name="IdC"
column="ID_C"
type="java.math.BigDecimal"
length="22"
/>

</composite-id>


<many-to-one
name="tableC"
class="com.mjwall.TableC"
update="false"
insert="false">
<column name="ID_A" />
<column name="ID_C" />
</many-to-one>

<many-to-one
name="tableB"
class="com.mjwall.TableB"
update="false"
insert="false">
<column name="ID_A" />
<column name="ID_B" />
</many-to-one>

</class>
</hibernate-mapping>

I need to add this the Middlegen Jira too I suppose.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 11:47 am 
Newbie

Joined: Tue Jan 06, 2004 3:49 pm
Posts: 11
Location: Montreal, Canada
Thanks for the info mjwall!


Top
 Profile  
 
 Post subject: insert problem
PostPosted: Sat Jan 31, 2004 3:11 pm 
Newbie

Joined: Fri Sep 19, 2003 2:57 pm
Posts: 10
When to set this parameters
update="false"
insert="false"

the SQL code doesn't include the relationship fields.

How could you do the insert and update operation for that situation?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 01, 2004 9:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
By updating the composite id.
You cannot map 2 proeprties into 1 column in Hibernate, one of them should be read-only: that's the purpose of insert="false" update="false"

_________________
Emmanuel


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