-->
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: managing relationships (foreign keys)
PostPosted: Thu Feb 21, 2008 3:47 pm 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi all,

I am trying to figure out how to map my database tables into hibernate XML.

This is my table structure
Code:
DROP TABLE IF EXISTS `db`.`session`;
CREATE TABLE  `db`.`session` (
  `id` int(11) NOT NULL auto_increment,
  `startTime` timestamp NOT NULL default '0000-00-00 00:00:00',
  `updateTime` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `remoteAddress` varchar(15) NOT NULL,
  `sessionId` varchar(63) NOT NULL,
  `browserName` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `sessionId` (`sessionId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;

DROP TABLE IF EXISTS `db`.`history`;
CREATE TABLE  `db`.`history` (
  `id` int(11) NOT NULL auto_increment,
  `sessionId` int(11) NOT NULL,
  `URL` text NOT NULL,
  `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `runtime` double NOT NULL,
  PRIMARY KEY  (`id`, `sessionId`),
  KEY (`runtime`),
  CONSTRAINT `historySessionId` FOREIGN KEY (`sessionId`) REFERENCES `session` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


And this is my hibernate XML for those classes
Code:
    <class name="Storage.Session" table="session">
   <id name="id">
       <generator class="native"/>
   </id>
   <property name="sessionId">
       <column name="sessionId" unique-key="SessionId"/>
   </property>
   <property name="startTime" type="timestamp"/>
   <property name="updateTime" type="timestamp"/>
   <property name="remoteAddress"/>
   <property name="browserName"/>
   
   <set name="history" table="history">
       <key column="sessionId"/>
       <many-to-many class="Storage.History"/>
   </set>
   <set name="search" table="search">
       <key column="sessionId"/>
       <many-to-many class="Storage.Search"/>
   </set>
   <one-to-one name="user" class="Storage.User"/>
    </class>

Code:
    <class name="Storage.History" table="history">
   <!--
   <id name="id">
       <generator class="native"/>
   </id>
   -->
   <composite-id>
       <key-property name="id"/>
       <key-property name="sessionId"/>
   </composite-id>
   <!--<property name="sessionId"/>-->
   <property name="url" column="URL"/>
   <property name="date" type="timestamp"/>
   <property name="runtime"/>
   
   <!--
   Properties that are linked to other tables ...
   -->
   
   <many-to-one name="session" class="Storage.Session"/>
    </class>



The error I get when I try to list items in the database is I'm using one primary key when I should be using 2, but I believe I am.

Code:
org.hibernate.MappingException: Foreign key (FK373FE494CAB2648E:history [elt])) must have same number of columns as the referenced primary key (history [sessionId,elt])


Thanks,
Walter


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.