1

I have a question about inheritance in JPA, is it possible to do this Hierarchy using JOINED strategy?

Here my code:

@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{
...
}

After the above hierarchy, I tried to make a query using JPQL, this is the query:

@Entity
@Table(name="FICHA_EVALUACION")
public class SheetEvaluation implements Serializable{
    @OneToOne
    private Teacher teacher;
}  

This code run from a main

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());

}

when I run the query throws me the following error

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'employee1_.DTYPE' in 'field list'

Please if you have any idea about the type of error obtained much I appreciate your support.

Best Regards

1 Answer 1

0

I am no hibernate guru, but I think You can't have @DiscriminatorColumn when you are using table-per-class, it is only required whne you have single class for all the inhereted classes and want to discriminate between them.

Check out this for single table join.

In you current join, It will create table for all the classe and will have person 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{
...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.