dimanche 19 avril 2015

Java Order by on one to many relationship with different child object type

Question


I need sort the ExamObjects according to the id if the ExamObject is of ExamTask and sort it according to the questionNumber if it's ExamQuestion. How can I do this?


Important


An exam will only have a set of ExamTask or ExamQuestion. In other words, one exam cannot have a mixture of ExamTask and ExamQuestion.


Background Information


I have an Entity class called Exam This class can contain one or more ExamObject entities.



@Entity
public class Exam {
@OneToMany(mappedBy = "exam" ...)
@OrderBy("id") //I need to order this by question number if its ExamQuestion
private Set<ExamObject> objects;
...
}


ExamObject can be of two types as below using JOINED



  1. ExamTask extends ExamObject

  2. ExamQuestion which extends ExamObject and has a column called questionNumber


ExamObject



@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class ExamObject {
@Id
private Long id;
...


ExamTask



@Entity
@PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id")
public class ExamTask extends ExamObject{
...


ExamQuestion



@Entity
@PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id")
public class ExamQuestion extends ExamObject{
@Column(name = "question_number")
private Integer questionNumber;
...

Aucun commentaire:

Enregistrer un commentaire