Hibernation request for superclass property

First of all, please forgive my ignorance in both Java and Hibernate, I am studying various ORM solutions and not a Java programmer.

1) Is it possible to map the following class hierarchy to a database table with Person.name and Employee.name pointing to different columns?

 abstract public class Person { private String name; } public class Employee extends Person { private String name; } 

2) Suppose the answer to 1) is Yes, is there a way to create an HQL or Criteria query that asks Hibernate to return Employee objects with the Person.name criterion?

Something like that:

 SELECT e FROM Employee e WHERE e.super.name = "test"; 
+4
source share
1 answer

1) Yes. This can be done using MappedSuperclass and annotate your columns

 @MappedSuperclass abstract public class Person { @Column(name="PERSON_NAME") private String name; } @Entity public class Employee extends Person { @Column(name="EMPLOYEE_NAME") private String name; } 

2) No. Without changing the attribute names of one of the persons or employees. However, you can query for all objects of a person with this name and pass the results to employees. Another option is to use Native SQL to query the column you want, again, probably not perfect.

 SELECT p FROM Person p WHERE p.name = "test"; 
+1
source

All Articles