-->
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: Newbie hbm.xml mapping question
PostPosted: Tue Aug 29, 2006 2:15 am 
Newbie

Joined: Sun Aug 06, 2006 12:55 am
Posts: 11
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?


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.