How to convert ORM to its subclass using Hibernate?

For example, I have two classes: Person and Employee (Employee is a subclass of Person). Person: has a name and a name. Employee: also has a salary.

On the client side, I have one form of HTML where I can fill in information about a person (for example, name and surname). I also have a “switch” between “Person” and “Employee”, and if the switch is turned on in Employee, I can fill in the salary field.

On the server side, servlets receive information from the client and use the Hibernate infrastructure to create / update data to / from the database. The display that I use is a separate table for individuals and employees with a discriminator.

I do not know how to convert Personto Employeeby changing it type, which is used as DiscriminatorColumn.

At first I tried:

  • load Person p from the database
  • create an empty Employee e object
  • copy values ​​from p to e
  • set salary value
  • save e in database

But I could not, since I also copied the identifier, and so Hibernate told me where there are two instances of ORM with the same identifier.

And I cannot pass Person directly to Employee, since Person is the superclass of the Employee class.

There seems to be a dirty way: remove the person and create an employee with the same information, but I really don't like it.

Therefore, I will be grateful for any help with this :)

Some notes:

Person Class:

public class Person {

    protected int id;

    protected String firstName;
    protected String lastName;

    // usual getters and setters
}

Employee Class:

public class Employee extends Person {

   // string for now
   protected String salary;

   // usual getters and setters
}

And in the servlet:

// type is the "switch"
if(request.getParameter("type").equals("Employee")) {
  Employee employee = daoPerson.getEmployee(Integer.valueOf(request.getParameter("ID")));
  modifyPerson(employee, request);
  employee.setSalary(request.getParameter("salary"));
  daoPerson.save(employee );
}
else {
  Person person = daoPerson.getPerson(Integer.valueOf(request.getParameter("ID")));
  modifyPerson(employee, request);
  daoPerson.save(person);
}

And finally, loading (in dao):

public Contact getPerson(int ID){
  Session session = HibernateSessionFactory.getSession();
  Person p = (Person) session.load(Person.class, new Integer(ID));
  return p; 
}

public Contact getEmployee(int ID){
  Session session = HibernateSessionFactory.getSession();
  Employee = (Employee) session.load(Employee.class, new Integer(ID));
  return p; 
}

ClassCastException Person getEmployee.

Hibernate XML:

<class name="domain.Person" table="PERSON" discriminator-value="P">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>

        <discriminator column="type" type="character"/>


        <property name="firstName" type="java.lang.String">
            <column name="FIRSTNAME" />
        </property>
        <property name="lastName" type="java.lang.String">
            <column name="LASTNAME" />
        </property>

  <subclass name="domain.Employee" discriminator-value="E">
    <property name="salary" column="SALARY" type="java.lang.String" />
  </subclass>
</class>

?: -/

+3
2

, bad bad hack, .

- Person Employee . , "" , String PERSON EMPLOYEE. , Person. "hackType".

@Column(columnName="disc")
private String hackType;

hackType . , hackType - EMPLOYEE . - "" Person , Person ( ), hackType EMPLOYEE, , ( ) .

, nHibernate, , (Java) Hibernate. - ,, .

, , , : Employee Person.. , Employee Employee Person ( ) . 1:1 . Person , Employee - Person. ( Employee - , EmploymentFacts, , Employee .)

+4

, . , "E" , getEmployee(), .

0

Source: https://habr.com/ru/post/1785872/


All Articles