I have a superclass that models an existing table called person that already contains data.
There is also a subclass Employee.
Now I want to be able to convert an existing person to an employee.
When saving an employee, Hibernate should check to see if there is a row in both the person and the employee table and then do a corresponding insert. Hibernate should only try to insert a row in the employee table since the person already exists in the person table.
Code:
@Entity
@Table(name = "person", uniqueConstraints = {
@UniqueConstraint(columnNames = {"person_firstname", "person_lastname"})
})
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable {
private String id;
private String title;
private String firstname;
private String lastname;
private char gender;
private String address;
private String email;
private String phone;
private String mobile;
}
@Entity
@Table(name="employee")
@PrimaryKeyJoinColumn(name="employee_id")
public class Employee extends Person {
private String position;
private String username;
private String password;
}
@Entity
@Table(name = "customer")
@PrimaryKeyJoinColumn(name="customer_id")
public class Customer extends Person {
private Bank bank;
private String bankAccount;
private byte[] signature;
// .....
}
How do I achieve this?
When calling getHibernateTemplate().saveOrUpdate(employee) I get the following:
Code:
11:48:03,835 DEBUG SQL:401 - /* get current state hakim.model.Employee */ select employee_.employee_id, employee_1_.person_address as person2_26_, employee_1_.person_email as person3_26_, employee_1_.person_firstname as person4_26_, employee_1_.person_gender as person5_26_, employee_1_.person_lastname as person6_26_, employee_1_.person_mobile as person7_26_, employee_1_.person_phone as person8_26_, employee_1_.person_title as person9_26_, employee_.password as password27_, employee_.position as position27_, employee_.salary as salary27_, employee_.username as username27_ from employee employee_ inner join person employee_1_ on employee_.employee_id=employee_1_.person_id where employee_.employee_id=?
11:48:03,839 DEBUG SQL:401 - /* insert hakim.model.Employee */ insert into person (person_address, person_email, person_firstname, person_gender, person_lastname, person_mobile, person_phone, person_title, person_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
11:48:03,841 WARN JDBCExceptionReporter:77 - SQL Error: 1062, SQLState: 23000
11:48:03,842 ERROR JDBCExceptionReporter:78 - Duplicate entry '1980112511' for key 1
As you can see Hibernate first tries to insert a row in the table person;
Is it possible to let Hibernate execute two selects (instead of the inner join) when querying the state of Employee (one for the person table and one for the employee table). Hibernate could then see that there is already a row in the person table and only needs to insert a row in the employee table.
When calling getHibernateTemplate().update(employee) however Hibernate only does an inner join select without inserting or updating any data:
Code:
12:31:54,301 DEBUG SQL:401 - /* get current state hakim.model.Employee */ select employee_.employee_id, employee_1_.person_address as person2_26_, employee_1_.person_email as person3_26_, employee_1_.person_firstname as person4_26_, employee_1_.person_gender as person5_26_, employee_1_.person_lastname as person6_26_, employee_1_.person_mobile as person7_26_, employee_1_.person_phone as person8_26_, employee_1_.person_title as person9_26_, employee_.password as password27_, employee_.position as position27_, employee_.salary as salary27_, employee_.username as username27_ from employee employee_ inner join person employee_1_ on employee_.employee_id=employee_1_.person_id where employee_.employee_id=?
One solution would be to delete the already existing person row and insert the employee.
This however is not going to work in my case since the person may already be linked to other data. A person can even be both an employee and a customer.
Please does anyone now a solution for this?