org.hibernate.WrongClassException:对象[id = null]不是指定的子类

时间:2018-09-22 19:35:01

标签: java spring hibernate spring-data-jpa

我具有如下数据模型

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

import org.hibernate.annotations.DiscriminatorOptions;

@SuppressWarnings("deprecation")
@Entity
@Table
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="DTYPE", discriminatorType=DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@DiscriminatorOptions(force=true)
public class Employee implements Serializable {


    private static final long serialVersionUID = 1L;

    public Employee() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "EMPLOYEE_ID")
    private int empID;

    private String firstName;

    private String lastName;

    private Integer age;

    private String email;

    private String city;

    private String phNum;


    public String getPhNum() {
        return phNum;
    }

    public void setPhNum(String phNum) {
        this.phNum = phNum;
    }

    public int getEmpID() {
        return empID;
    }

    public void setEmpID(int empID) {
        this.empID = empID;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}

Patient.java

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.DiscriminatorOptions;
import org.hibernate.validator.constraints.NotEmpty;

@SuppressWarnings("deprecation")
@Entity
@Table
@PrimaryKeyJoinColumn(name = "EMPLOYEE_ID")
@DiscriminatorValue("6")

public class Patient extends Employee {


    private static final long serialVersionUID = 1L;

    @NotEmpty(message = "DOJ cannot be null")
    private String doj;

    private String primaryDoctor;


    public String getPrimaryDoctor() {
        return primaryDoctor;
    }

    public void setPrimaryDoctor(String primaryDoctor) {
        this.primaryDoctor = primaryDoctor;
    }

    public String getDoj() {
        return doj;
    }

    public void setDoj(String doj) {
        this.doj = doj;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "patient", fetch = FetchType.LAZY)
    private List<Encounter> encounterList;

    public List<Encounter> getEncounterList() {
        return encounterList;
    }

    public void setEncounterList(List<Encounter> encounterList) {
        this.encounterList = encounterList;
    }

}

运行时休眠查询

select useraccoun0_.empID as empID1_13_2_, useraccoun0_.EMPLOYEE_ID as EMPLOYEE6_13_2_, 
useraccoun0_.fullName as fullName2_13_2_, useraccoun0_.password as password3_13_2_, 
useraccoun0_.role as role4_13_2_, useraccoun0_.userName as userName5_13_2_, 
employee1_.EMPLOYEE_ID as EMPLOYEE2_2_0_, employee1_.age as age3_2_0_,
 employee1_.city as city4_2_0_, employee1_.email as email5_2_0_, employee1_.firstName as firstNam6_2_0_,
  employee1_.lastName as lastName7_2_0_, employee1_.phNum as phNum8_2_0_, employee1_4_.doj as doj1_11_0_,
   employee1_4_.primaryDoctor as primaryD2_11_0_, employee1_5_.specialization as speciali1_0_0_,
     employee1_.DTYPE as DTYPE1_2_0_, encounterl2_.EMPLOYEE_ID as EMPLOYEE4_3_4_, 
     encounterl2_.EID as EID1_3_4_, encounterl2_.EID as EID1_3_1_, encounterl2_.labTest_testID as labTest_2_3_1_,
      encounterl2_.medication_mid as medicati3_3_1_, encounterl2_.EMPLOYEE_ID as EMPLOYEE4_3_1_,
       encounterl2_.vitalSign_EID as vitalSig5_3_1_ from UserAccount useraccoun0_ 
        left outer join Employee employee1_ on useraccoun0_.EMPLOYEE_ID=employee1_.EMPLOYEE_ID
         left outer join Pharmacist employee1_1_ on employee1_.EMPLOYEE_ID=employee1_1_.EMPLOYEE_ID 
         left outer join Nurse employee1_2_ on employee1_.EMPLOYEE_ID=employee1_2_.EMPLOYEE_ID 
         left outer join LabAssistant employee1_3_ on employee1_.EMPLOYEE_ID=employee1_3_.EMPLOYEE_ID 
         left outer join Patient employee1_4_ on employee1_.EMPLOYEE_ID=employee1_4_.EMPLOYEE_ID 
         left outer join Doctor employee1_5_ on employee1_.EMPLOYEE_ID=employee1_5_.EMPLOYEE_ID 
         left outer join Encounter encounterl2_ on employee1_.EMPLOYEE_ID=encounterl2_.EMPLOYEE_ID
          where useraccoun0_.empID=4

在此查询中,鉴别符列(DTYPE)即将到来,其值即将为6。但这给了我以下例外情况

  

org.hibernate.WrongClassException:对象[id = null]不是指定的子类[com。*。*。Employee]:给定对象的类与持久复制的类不匹配

所以不确定为什么会给我这个异常,为什么id会为null。

0 个答案:

没有答案