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 ... AskForHelpHibernate 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]