Hi all
I have a db with the following SQL DDL
Code:
CREATE TABLE `person` (
`person_id` int(10) unsigned NOT NULL auto_increment,
`surname` varchar(45) NOT NULL default '',
`first_names` varchar(45) NOT NULL default '',
PRIMARY KEY (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `player` (
`player_id` int(10) unsigned NOT NULL auto_increment,
`person_id` int(10) unsigned NOT NULL default '0',
`experience` int(10) unsigned NOT NULL default '0',
`age` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`player_id`),
UNIQUE KEY `fk_player_person` (`person_id`),
KEY `fk_player_region` (`region_id`),
CONSTRAINT `fk_player_region` FOREIGN KEY (`region_id`) REFERENCES `region` (`region_id`),
CONSTRAINT `fk_player_person` FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
A person can be 0 or 1 player. A player cannot exist without a person being created first.
I have used the hibernate tools to generate Person and Player model classes. Relevent hbm.xml is below
Code:
<class name="com.mk.cricket.model.Person" table="person" catalog="cricket2">
<id name="id" type="integer">
<column name="person_id" />
<generator class="identity"></generator>
</id>
<property name="surname" type="string">
<column name="surname" length="45" not-null="true">
<comment></comment>
</column>
</property>
<property name="firstNames" type="string">
<column name="first_names" length="45" not-null="true">
<comment></comment>
</column>
</property>
</class>
<class name="com.mk.cricket.model.Player" table="player" catalog="cricket2">
<id name="id" type="integer">
<column name="player_id" />
<generator class="identity"></generator>
</id>
<many-to-one name="person" class="com.mk.cricket.model.Person" fetch="join" lazy="false">
<column name="person_id" not-null="true" unique="true"/>
</many-to-one>
<property name="experience" type="integer">
<column name="experience" not-null="true"/>
</property>
<property name="age" type="integer">
<column name="age" not-null="true"/>
</property>
</class>
I am able to navigate from player to person in my player class, but i'd also like to add a player object reference to my person class.
The problem I am having is that, as there is no player_id column in the person table, I dont really know how to write the mapping xml that will allow me to add a player field to my person class.
Can anyone help me out?