-->
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 Many-To-Many (to many?!?) ??
PostPosted: Thu Apr 03, 2008 9:47 am 
Newbie

Joined: Mon Jul 23, 2007 11:58 am
Posts: 15
Location: New Jersey
I am having a situation in which I need some help...

As you can tell from the subject of this post - I am a bit confused.

As you read my issue, let me forewarn you that I am still fairly a beginner with the hibernate framework.


Let me describe the scenario.

First the entities in my system...
I have a Developer, a ProgramLanguage and a Framework...(hope this doesn't confuse matters).

Ok, so how do these entities relate?

Well I figure a ProgramLanguage can be supported by one or more Frameworks.

A Framework can support One or more ProgramLanguages.

Example: Hibernate supports java and .net. Java is supported by Hibernate and Spring

This is my first many-to-many relationship. So if I go back in time to Database Modeling 101, this will tell me that I will have to set up an association table.

Ok, so now lets introduce the associations for developers. Lets say we want to capture all the Frameworks/Language combos a developer likes to use. Therefore, we say...

A developer likes to use one or more Frameworks/Language.
A Framework/Language is used by one or more developers.

Another many to many...

Ok, now let me create the tables...just paraphrasing here...

CREATE TABLE Developer(ID, NAME);
CREATE TABLE ProgramLanguage(ID, NAME);
CREATE TABLE Framework(ID, NAME);

Now the association tables...
CREATE TABLE ProgramLanguage_Framework(ID, ProgramLanguageId, FrameworkId)

CREATE TABLE DEVELOPER_PROGRAMLANGUAGE_FRAMEWORK(ID, ProgramLanguageFrameworkId, DeveloperId)

Here is a sample dataset...
Developer
ID | NAME
1 | Roy
2 | Gavin

ProgramLanguage
ID | NAME
1 | Java
2 | .Net

Framework
ID | NAME
1 | Hibernate
2 | Spring

ProgramLanguage_Framework
ID | ProgramLanguageId, FramworkId
1 | 1 (Java) | 1 (Hibernate)
2 | 2 (Java) | 2 (Spring)
3 | 3 (.Net) | 1 (Hibernate)

DEVELOPER_PROGRAMLANGUAGE_FRAMEWORK
ID | ProgramLanguageFrameworkId | DeveloperId
1 | 1 (Java and Hibernate) | 1 (Roy)
2 | 1 (Java and Hibernate) | 2 (Gavin)
3 | 2 (Java and Spring) | 1 (Roy)
4 | 3 (.Net and Hibernate) | 2 (Gavin)

So this is how I understood to Map these relationships in hibernate...

Code:
<class name="Developer" table="Developer">
         <id name="id" column="id">
             <generator class="org.hibernate.id.IncrementGenerator"/>
         </id>
         
         <set name="frameworksUsed"  table="DEVELOPER_PROGRAMLANGUAGE_FRAMEWORK">
             <key column="DeveloperId"/>
             <many-to-many column="ProgramLanguageFrameworkId " 
class="ProgramLanguageFramework"/>
         </set>
</class>


   <class name="Framework" table="Framework">
         <id name="id" column="Id">
             <generator class="org.hibernate.id.IncrementGenerator"/>
         </id>
         
         <set name="programLanguages" table="ProgramLanguage_Framework">
             <key column="FrameworkId"/>
             <many-to-many column="ProgramLanguageId" 
class="ProgramLanguage"/>
         </set>
     </class>


     <class name="ProgramLanguage" table="ProgramLanguage">
         <id name="id" column="Id">
             <generator class="org.hibernate.id.IncrementGenerator"/>
         </id>
         
         <set name="frameworks" inverse="true" table="ProgramLanguage_Framework">
             <key column="ProgramLanguageId"/>
             <many-to-many column="FrameworkId"  class="Framework"/>
         </set>
     </class>



     <class name="ProgramLanguageFramework" 
table="ProgramLanguage_Framework">
         <id name="id" column="id">
                 <generator 
class="org.hibernate.id.IncrementGenerator"/>
         </id>
     
         <set name="developersWhoLike" inverse="true" 
table="DEVELOPER_PROGRAMLANGUAGE_FRAMEWORK">
             <key column="ProgramLanguageFrameworkId"/>
             <many-to-many column="DeveloperId"  class="Developer"/>
         </set>
     </class>

     <class name="DeveloperProgramLanguageFramework" 
table="ProfileCategoryActivity">
            <id name="id" column="id">
                    <generator 
class="org.hibernate.id.IncrementGenerator"/>
            </id>
           
     </class>




When Hibernate starts...
I get the following exception upon mapping...
Code:
Caused by: org.hibernate.MappingException: Foreign key 
(FKA0A469D6ADE4DB54:DeveloperProgramLanguageFramework [ProgramLanguageFrameworkId])) 
must have same number of columns as the referenced primary key 
(ProgramLanguageFramework [FrameworkId,ProgramLanguageId])


I am 100% sure that [FrameworkId,ProgramLanguageId] is not the primary key of ProgramLanguageFramework.

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0

Mapping documents:

Code between sessionFactory.openSession() and session.close():
N/A
Full stack trace of any exception that occurs:
Code:
Caused by: org.hibernate.MappingException: Foreign key 
(FKA0A469D6ADE4DB54:ProfileCategoryActivity [CategoryActivityId])) 
must have same number of columns as the referenced primary key 
(CategoryActivity [CategoryId,ActivityId])
       at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
       at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
       at 
org
.hibernate
.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1187)
       at 
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:
1094)
       at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:
1039)
       at 
org
.springframework
.orm
.hibernate3
.LocalSessionFactoryBean
.buildSessionFactory(LocalSessionFactoryBean.java:675)
       at 
org
.springframework
.orm
.hibernate3
.AbstractSessionFactoryBean
.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
       at 
org
.springframework
.beans
.factory
.support
.AbstractAutowireCapableBeanFactory
.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1333)
       at 
org
.springframework
.beans
.factory
.support
.AbstractAutowireCapableBeanFactory
.initializeBean(AbstractAutowireCapableBeanFactory.java:1299)
       ... 151 more

Name and version of the database you are using:
MySql 5.x
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 4:41 pm 
Newbie

Joined: Mon Jul 23, 2007 11:58 am
Posts: 15
Location: New Jersey
does anyone have an idea why this might be happening...The primary key is definitely not what hibernate thinks it is...

All I want is to be able to find out which frameworks/Languages a Developer likes...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 10:14 pm 
Newbie

Joined: Mon Jul 23, 2007 11:58 am
Posts: 15
Location: New Jersey
I remember at one point I did have a composite key definied. I had the two columns hibernate is looking created as UNIQUE in mysql...Is it possible that Hibernate is holding on to some sort of cached version of that mapping definition. I am going crazy on this one...The mappings all seem correct, but i am still getting that exception. I just don't get it...


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.