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.