I’m trying to map some objects in Hibernate. One of these objects is father and the others are children. In other words they implement inheritance.
The father is as follow:
public class Person {
private String id;
private String name;
private String surname;
getters and setters ;
}
and children…
public class Employee {
private BigDecimal salary;
private String seccion;
private Employee employee;
private Customer customer;
getters and setters
}
public class Customer {
private BigDecima CreditLimit;
getter and setter
}
Then… I want to map these classes in the following database schema…
Table
Person
ID / NAME / SURNAME / ID_EMPLOYEE / ID_CUSTOMER
Employee
ID_PERSON / SALARY / SECCION
Customer
ID_PERSON / CREDIT_LIMIT
My idea is each persona can be or not a customer/employee. In other words Customer and Employee are properties of Person but these properties will be store in independents tables in the database.
For get the credit limit of a persona I can do persona.getCustomer().getCreditLimit();
Always making control if the Person is a Customer or is not.
I hope you can help me and excuse me my English is pretty poor. I’m from Argentina.
Thanks in advance.
Nicolas
You could map that with two
One-To-Oneassociations onPerson.As a side note, if you’ve got control over that schema, I’d reccomend going for Inheritance Mapping and Table-per-subclass, using a
typecolumn as discriminator on the person table. Here is a tutorial on inheritance mapping.