Java – hibernate query on superclass properties

First of all, please forgive my ignorance in Java and hibernate. I'm studying different ORM solutions, not Java programmers

1) Can you map the following class hierarchy to the database table, person Name and employee Name points 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 any way to create an HQL or criteria query that requires hibernate to return the employee object and display it in person Is there a standard on name?

Something like this:

SELECT e FROM Employee e WHERE e.super.name = "test";

Solution

1) Yes This can be done with 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 Do not change the property name of one of person or employee However, you can use this name to query all personnel objects and cast the results to employees Another option is to use native SQL to query the required columns, which may not be ideal

SELECT p FROM Person p WHERE p.name = "test";
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>