I have two abstract class and two concrete class for both the abstract class. Question class abstraction is working as expected but choice is not working as expected and throwing below error:
javax.persistence.PersistenceException: org.hibernate.WrongClassException: Object with id: 644b7a9d-4d92-4120-af41-8067a321bd1a was not of the specified subclass: com.xyz.model.Choice (Discriminator: RCO)
Here is how my database records looks like: Question Table: dtype: uuid RNQ : "cc8aa5f7-a8ff-42b5-9079-ce099368b4fa" CEQ : "b3a7e603-6e79-46ce-bb4f-3a9a152e0407"
Choice Table: dtype: uuid : question_uuid RCO : "644b7a9d-4d92-4120-af41-8067a321bd1a" : "cc8aa5f7-a8ff-42b5-9079-ce099368b4fa" CHO : "b20dce82-3b9d-4841-808f-bfe98edeade6" : "b3a7e603-6e79-46ce-bb4f-3a9a152e0407"
Notice choice question is working with correct choices while ranking question is throwing the above error and does not associate rankchoice object instead it's trying to associate choice object.
This is how objects looks like:
@XmlRootElement @XmlSeeAlso({Choice.class, RankChoice.class}) @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, length = 3) @DiscriminatorValue("CCO") @Table(name = "choice") public abstract class AbstractChoice { ---some fields ---some fields }
Concrete class 1:
@Entity @DiscriminatorValue("CHO") public class Choice extends AbstractChoice{ private ChoiceQuestion question; }
Concrete class 2: @Entity @DiscriminatorValue("RCO") public class RankChoice extends AbstractChoice{
private RankQuestion question; }
Another abstract class
@XmlRootElement @XmlSeeAlso({ChoiceQuestion.class, RankQuestion.class}) @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, length = 3) @DiscriminatorValue("QES") @Table(name = "question") public abstract class Question { ---Some fields }
Concrete class 1:
@XmlRootElement @Entity @DiscriminatorValue("CEQ") public class ChoiceQuestion extends Question { private List<Choice> choices;
@XmlElementRefs({ @XmlElementRef(type = Choice.class) }) @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}) @OrderColumn(name="choice_index") @JoinColumn(name = "question_uuid") public List<Choice> getChoices() { return choices; } }
Concrete class 2:
@XmlRootElement @Entity @DiscriminatorValue("RNQ") public class RankQuestion extends Question { private List<RankChoice> choices;
@XmlElementRefs({ @XmlElementRef(type = RankChoice.class)
}) @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}) @OrderColumn(name="choice_index") @JoinColumn(name = "question_uuid") public List<RankChoice> getChoices() { return choices; } }
|