I'm trying to create an address book application with one table for People, another for Companies and a third one for Phones. The Phones table will have the data for the People and the Companies with a field indicating where it came from. Let's assume that these are the simple tables:
CREATE TABLE `Person` {
`id` int(11) NOT NULL auto_increment,
`firstName` varchar(80) NOT NULL,
`lastName` varchar(80) NOT NULL
}
CREATE TABLE `Company` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(80) NOT NULL
}
CREATE TABLE `ContactPhone` (
`id` int(11) NOT NULL auto_increment,
`contactType` char(1) NOT NULL, -- either 'P' or 'C'
`contactId` int(11) NOT NULL, -- the id of the Person or Company
`number` varchar(40) NOT NULL
}
I have created their corresponding java classes which are a very simple representation:
public class Person {
private Integer id;
private String firstName;
private String lastName;
private Collection<Phone> phones;
// ...
}
public class Company {
private Integer id;
private String name;
private Collection<Phone> phones;
// ...
}
public class Phone {
private Integer id;
private String number;
// ...
}
In plain SQL I can get the person and corresponding phones very easily. I need to create a mapping between Person and Phone and between Company and Phone using plain hibernate (no annotations) but can't get it to work. Not even sure if it has to be unidirectional or bidirectional. I have searched for the documentation but haven't found anything that can help. Anyone?