我对JPA中的继承有疑问,是否可以使用JOINED策略执行此层次结构?
这是我的代码:
@Entity
@Table(name = "PERSON")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING, length = 4)
public abstract class Person implements Serializable {
...
}
@Entity
@Table(name = "EMPLOYEE")
@DiscriminatorValue(value="EMPL")
public abstract class Employee extends Person implements Serializable{
...
}
@Entity
@Table(name="CLIENT")
@DiscriminatorValue(value="CLIE")
public class Client extends Person implements Serializable{
...
}
@Entity
@Table(name="TEACHER")
@DiscriminatorValue(value="TEAC")
public class Teacher extends Employee implements Serializable{
...
}
在上面的层次结构之后,我尝试使用JPQL进行查询,这是查询:
@Entity
@Table(name="FICHA_EVALUACION")
public class SheetEvaluation implements Serializable{
@OneToOne
private Teacher teacher;
}
此代码从主
运行Query query = entityManager.createQuery("SELECT f FROM SheetEvaluation f, Teacher teac, Employee emp, Person per WHERE f.teacher.id = teac.id and teac.id = emp.id and emp.id = per.id");
List<SheetEvaluation> sheetEvaluations = query.getResultList();
for (SheetEvaluation sheetEvaluation : sheetEvaluations ) {
System.out.println(" Teacher Name= " + sheetEvaluation .getTeacher().getName());
}
当我运行查询时会抛出以下错误
引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'employee1_.DTYPE'
如果您对所获得的错误类型有任何了解,请感谢您的支持。
最好的问候
答案 0 :(得分:0)
我不是一个休眠大师,但我认为当你使用每个类的表时,你不能拥有@DiscriminatorColumn
,只有当你拥有所有固有类的单个类并且想要区别时才需要它它们之间。
查看this单表加入。
在你当前的加入中,它将为所有的classe创建表格并且将有人fk。
@Entity
@Table(name = "PERSON")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable {
...
}
@Entity
@Table(name = "EMPLOYEE")
public class Employee extends Person implements Serializable{
...
}
@Entity
@Table(name="CLIENT")
public class Client extends Person implements Serializable{
...
}
@Entity
@Table(name="TEACHER")
public class Teacher extends Employee implements Serializable{
...
}