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