-->
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.  [ 4 posts ] 
Author Message
 Post subject: MappingException Foreign key
PostPosted: Mon May 30, 2005 8:08 pm 
Newbie

Joined: Mon May 30, 2005 7:00 pm
Posts: 3
Hibernate version: 3

Background: A registry can contain many registry items. So one registry maps to many registy items. I have spent over 6 hours today looking up how to solve the problem, but cannot find a solution.

Problem: This error message is recieved at runtime when I first try to create a SessionFactory:

Foreign key (FK2446A9D5E9C1910E:REGISTRY_ITEM [ITEM_ID])) must have same number of columns as the referenced primary key (REGISTRY_ITEM [REGISTRY_ID,ITEM_ID])


Here are my tables (MySQL 4.x):

Registry
Code:
CREATE TABLE `REGISTRY` (
  `REGISTRY_ID` int(11) NOT NULL default '0',
  `OWNER` varchar(50) NOT NULL default '',
  `TITLE` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`REGISTRY_ID`)
) TYPE=InnoDB


Registry Item
Code:
CREATE TABLE `REGISTRY_ITEM` (
  `ITEM_ID` bigint(20) NOT NULL default '0',
  `REGISTRY_ID` int(11) NOT NULL default '0',
  `ITEM_NAME` varchar(200) NOT NULL default '',
  `DESCRIPTION` text,
  `IS_BOUGHT` int(1) NOT NULL default '0',
  `WANT_FACTOR` int(1) default NULL,
  PRIMARY KEY  (`ITEM_ID`),
  KEY `REGISTRY_ID` (`REGISTRY_ID`),
  CONSTRAINT `REGISTRY_ITEM_ibfk_1` FOREIGN KEY (`REGISTRY_ID`)                           REFERENCES `REGISTRY` (`REGISTRY_ID`)
) TYPE=InnoDB


My two mapping files.
Registry
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">

<hibernate-mapping>
  <class name="com.blakenetexpress.registry.registry.RegistryImpl" table="REGISTRY">
    <id name="id" column="REGISTRY_ID" type="long">
      <generator class="increment"/>
    </id>
    <property name="owner" column="OWNER" />
    <property name="title" column="TITLE" />
    <set name="items">
       <key column="REGISTRY_ID" not-null="true"/>
       <one-to-many
       class="com.blakenetexpress.registry.registry.RegistryItemImpl"/>
   </set>
  </class>
</hibernate-mapping>


RegistryItem
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">

<hibernate-mapping>
  <class name="com.blakenetexpress.registry.registry.RegistryItemImpl" table="REGISTRY_ITEM">
    <id name="id" column="ITEM_ID">
      <generator class="increment"/>
    </id>
    <property name="isBought" column="IS_BOUGHT" type="boolean"/>
    <property name="itemName" column="ITEM_NAME" />
    <property name="description" column="DESCRIPTION" />
    <property name="wantFactor" column="WANT_FACTOR" />
    <many-to-one name="registryId" 
              column="REGISTRY_ID"/>
  </class>
</hibernate-mapping>


Thank you to anyone who can help. This has been driving me crazy! Oh yeah, please take it easy on me as I am probably considered a beginner-intermediate in DB, and a beginner at hibernate :-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 12:13 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi

Try changing this

`ITEM_ID` int(11) NOT NULL default '0',

in the DB.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 4:18 pm 
Newbie

Joined: Mon May 30, 2005 7:00 pm
Posts: 3
ramnath wrote:
Hi

Try changing this

`ITEM_ID` int(11) NOT NULL default '0',

in the DB.


I wouldn't think that would affect it since ITEM_ID is only referenced in the one table, but I gave it a shot and have the same error.

Any other suggestions? Can anyone explain what the error means? I've put too much time into hibernate to back down now :-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 4:30 pm 
Newbie

Joined: Mon May 30, 2005 7:00 pm
Posts: 3
This has been solved. I was an idiot (yes, you heard it here :)). In my hibernate config file i had it point to a mapping file that I no longer wanted (it was one of my first test versions at mapping).

Thank you all for reading this and trying to help me out. I will try and return the favor if I can (and get more experienced).

-Blake


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