-->
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.  [ 1 post ] 
Author Message
 Post subject: Mapping exception
PostPosted: Thu Apr 29, 2010 1:21 pm 
Newbie

Joined: Thu Apr 29, 2010 1:06 pm
Posts: 1
Hey everyone,

I am new to hibernate and I am getting this annoying exception

org.hibernate.MappingException: Foreign key (FK9FBB8F107E0786C:Events_BelongsTo_Projects [p_id])) must have same number of columns as the referenced primary key (Projects [projectID,p_id])

I have 2 tables, Projects and Events, and there's a many to many relationship between them, here are the Mysql scripts
Code:
CREATE TABLE `Events`(

   `e_id` int(11) auto_increment,

   `name` VARCHAR(20) NOT NULL ,

   `location` VARCHAR(50) NOT NULL,

   `type` INT(1) NOT NULL,            #see note 3

   `description` VARCHAR(1500) DEFAULT NULL,

   `startTime` DATETIME NOT NULL,

   `endTime` DATETIME NOT NULL,

   `created_by` int(11),

   PRIMARY KEY(`e_id`),

   FOREIGN KEY(`created_by`) REFERENCES `Users`(`u_id`) ON DELETE SET NULL ON UPDATE CASCADE)

   ENGINE=INNODB DEFAULT CHARSET=Latin1;



#PROJECTS

CREATE TABLE `Projects`(

    `p_id` INT(11) NOT NULL AUTO_INCREMENT,

    `name`VARCHAR(100) NOT NULL,

    `type` INT(1) CHECK(type between 0 and 1),      #see note 4

    `budget` DOUBLE,

    `effort_estimation_unit`VARCHAR(100),

    `description` VARCHAR(1500),

    `projectID` INT(11),

    `sprint_duration` int(4) CHECK(sprint_duration between 7 and 62),

    PRIMARY KEY(`p_id`),

    FOREIGN KEY(`projectID`)REFERENCES `Projects`(`p_id`) ON DELETE CASCADE ON UPDATE SET NULL

    )ENGINE=INNODB DEFAULT Charset = Latin1;


CREATE TABLE `Events_BelongsTo_Projects`(

   `e_id` int(11),

   `p_id` int(11),

   PRIMARY KEY (`e_id`,`p_id`),

   FOREIGN KEY (`e_id`) REFERENCES `Events`(`e_id`) ON DELETE CASCADE ON UPDATE CASCADE,

   FOREIGN KEY (`p_id`) REFERENCES `Projects`(`p_id`) ON DELETE CASCADE ON UPDATE CASCADE)

   ENGINE=INNODB DEFAULT charset = Latin1;



and here are the hibernate xml mapping files
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 28, 2010 10:29:51 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="com.example.mega_scrum.newPersistenceClasses.Event" table="Events">
        <id name="id" type="int">
            <column name="e_id" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" />
        </property>
        <property name="location" type="java.lang.String">
            <column name="location" />
        </property>
        <property name="type" type="short">
            <column name="type" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="description" />
        </property>
        <property name="startTime" type="java.util.Date">
            <column name="startTime" />
        </property>
        <property name="endTime" type="java.util.Date">
            <column name="endTime" />
        </property>
        <!--<many-to-one name="creator" class="com.example.mega_scrum.newPersistenceClasses.User" fetch="join">
            <column name="created_by" />
        </many-to-one>
        <set name="tasks" table="Events_Tasks" inverse="false" lazy="extra">
            <key>
                <column name="e_id" />
            </key>
            <many-to-many column="t_id" class="com.example.mega_scrum.newPersistenceClasses.Task" />
        </set>-->
        <set name="notes" table="Notes" inverse="false" lazy="extra">
            <key>
                <column name="e_id" />
            </key>
            <one-to-many class="com.example.mega_scrum.newPersistenceClasses.Note" />
        </set>
        <set name="snapshots" table="Events_Snapshots" inverse="false" lazy="extra">
            <key>
                <column name="e_id" />
            </key>
            <one-to-many class="com.example.mega_scrum.newPersistenceClasses.EventSnapshot" />
        </set>
        <!--<set name="userStories" table="Events_UserStories" inverse="false" lazy="extra">
            <key>
                <column name="e_id" />
            </key>
            <many-to-many column="us_id" class="com.example.mega_scrum.newPersistenceClasses.UserStory" />
        </set>-->
        <set name="projects" table="Events_BelongsTo_Projects" inverse="true" lazy="extra">
            <key>
                <column name="e_id" />
            </key>
            <many-to-many column="p_id" class="com.example.mega_scrum.newPersistenceClasses.Project" />
        </set>
        <set name="dependantEvents" table="Events_Events" inverse="false" lazy="extra">
            <key>
                <column name="e_id" />
            </key>
            <many-to-many column="e_id_dependant" class="com.example.mega_scrum.newPersistenceClasses.Event" />
        </set>
        <!--<set name="boardColumns" table="Board_Columns" inverse="false" lazy="extra">
            <key>
                <column name="event_id" />
            </key>
            <one-to-many class="com.example.mega_scrum.newPersistenceClasses.BoardColumn" />
        </set>-->
    </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 28, 2010 10:29:51 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="com.example.mega_scrum.newPersistenceClasses.Project" table="Projects">
        <id name="id" type="int">
            <column name="p_id" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" />
        </property>
        <property name="type" type="int">
            <column name="type" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="description" />
        </property>
        <property name="budget" type="double">
            <column name="budget" />
        </property>
        <property name="effortEstimationUnit" type="java.lang.String">
            <column name="effort_estimation_unit" />
        </property>
        <property name="sprintDuration" type="int">
            <column name="sprint_duration" />
        </property>
        <set name="components" table="Projects" inverse="false" lazy="extra">
            <key>
                <column name="p_id" />
            </key>
            <one-to-many class="com.example.mega_scrum.newPersistenceClasses.Project" />
        </set>
        <!--<set name="roles" table="Roles" inverse="false" lazy="extra">
            <key>
                <column name="p_id" />
            </key>
            <one-to-many class="com.example.mega_scrum.newPersistenceClasses.Role" />
        </set>
        <set name="taskStatus" table="Task_Status" inverse="false" lazy="extra">
            <key>
                <column name="project_id" />
            </key>
            <one-to-many class="com.example.mega_scrum.newPersistenceClasses.TaskStatus" />
        </set>
        <set name="projectUserstories" table="UserStories_BelongsTo_Projects" inverse="false" lazy="extra">
            <key>
                <column name="p_id" />
            </key>
            <many-to-many column="u_id" class="com.example.mega_scrum.newPersistenceClasses.UserStory" />
        </set>
        <set name="dependantUserstories" table="UserStories_DependsOn_Projects" inverse="false" lazy="extra">
            <key>
                <column name="p_id" />
            </key>
            <many-to-many column="us_id" class="com.example.mega_scrum.newPersistenceClasses.UserStory" />
        </set>
        <set name="componentUserstories" table="UserStories" inverse="false" lazy="extra">
       
            <key>
                <column name="p_id" />
            </key>
            <one-to-many class="com.example.mega_scrum.newPersistenceClasses.UserStory" />
        </set>-->
        <set name="events" table="Events_BelongsTo_Projects" inverse="true" lazy="extra">
            <key>
                <column name="p_id" />
            </key>
            <many-to-many column="e_id" class="com.example.mega_scrum.newPersistenceClasses.Event" />
        </set>
        <set name="logs" table="Log_Projects" inverse="false" lazy="extra">
            <key>
                <column name="p_id" />
            </key>
            <many-to-many column="l_id" class="com.example.mega_scrum.newPersistenceClasses.Log" />
        </set>
    </class>
</hibernate-mapping>


here's the config file
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/se_megasoft</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
   <mapping resource="Event.hbm.xml"/>
   <mapping resource="Project.hbm.xml"/>   
    </session-factory>
</hibernate-configuration>


any help would be great.
Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.