-->
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: Getting Foreign key from another table - many-to-one
PostPosted: Mon May 22, 2006 4:18 am 
Newbie

Joined: Mon May 22, 2006 2:47 am
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1.3

Name and version of the database you are using: MySql 5


Hi,

I have 2 tables:

CREATE TABLE `Person` (
`Id` int(11) NOT NULL auto_increment,
`name` varchar(11) default NULL,
`phone` varchar(11) default NULL,
`address_id` int(11) default NULL,
PRIMARY KEY (`Id`),
FOREIGN KEY (`address_id`) REFERENCES Addresses(`id`)
);

CREATE TABLE `Addresses` (
`Id` int(11) NOT NULL default '0',
`phone` varchar(11) default NULL,
`address` varchar(255) default NULL,
PRIMARY KEY (`Id`)
)

<class name="PersonItem" table="Person">
<id name="id" column="id" unsaved-value="null">
<generator class="uuid"/>
</id>
.....

<many-to-one name="addressItem" class="AddressItem" column="address_id" not-null="false" unique="false" lazy="false" cascade="persist,save-update,refresh"/>

</class>

The address table is a collection of phone numbers and addresses. When I insert a person in the table, I want to check whether the person's phone number is in the Addresses table, if yes, I want to populate the Person.address_id field with the primary key of the addresses table.

What I thought of doing is before I insert a new person in the person table, I would check the addresses table for a match so I can get the Addresses.address_id and I would then insert the person in the person table with the address_id but hibernate would not let me update or insert into the Person.address_id field.

If there a (smarter) way of doing it?please help.


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.