Assignment.java
@Entity
@Table(name = "assignments")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Assignment {
...
}
QuizAssignment.java (Parent)
@Entity
@Table(name = "quiz_assignments")
@PrimaryKeyJoinColumn(name = "id")
public class QuizAssignment extends Assignment {
private List<QuizQuestion> quizQuestions;
public QuizAssignment() {
super();
}
public QuizAssignment(int id, String name, String lecturerId, String studentId,
Date startDate, Date endDate, List<QuizQuestion> quizQuestions) {
super(id, name, lecturerId, studentId, startDate, endDate);
this.quizQuestions = quizQuestions;
}
@JsonIgnore
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@OneToMany(mappedBy = "assignment", fetch = FetchType.EAGER)
public List<QuizQuestion> getQuizQuestions() {
return quizQuestions;
}
public void setQuizQuestions(List<QuizQuestion> quizQuestions) {
this.quizQuestions = quizQuestions;
}
}
QuizQuestion.java (Child)
@Entity
@Table(name = "quiz_questions")
public class QuizQuestion {
private int quiz_id;
private String question;
private String choices;
private String answer;
private Assignment assignment;
public QuizQuestion() {}
public QuizQuestion(int quiz_id, String question, String choices, String answer, Assignment assignment) {
this.quiz_id = quiz_id;
this.question = question;
this.choices = choices;
this.answer = answer;
this.assignment = assignment;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "quiz_id")
public int getQuizId() {
return quiz_id;
}
public void setQuizId(int quiz_id) {
this.quiz_id = quiz_id;
}
@Column(name = "question")
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
@Column(name = "choices")
public String getChoices() {
return choices;
}
public void setChoices(String choices) {
this.choices = choices;
}
@Column(name = "answer")
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id")
public Assignment getAssignment() {
return assignment;
}
public void setAssignment(Assignment assignment) {
this.assignment = assignment;
}
AssignmentService.java
public void save(Assignment a) {
dao.openCurrentSessionWithTransaction();
dao.save(a);
dao.closeCurrentSessionWithTransaction();
}
AssignmentDAO.java
@Override
public void save(Assignment entity) {
getCurrentSession().save(entity);
}
...
public Session openCurrentSessionWithTransaction() {
currentSession = HibernateConfig.getSessionFactory().openSession();
currentTransaction = currentSession.beginTransaction();
return currentSession;
}
public void closeCurrentSessionWithTransaction() {
currentTransaction.commit();
currentSession.close();
}
Hibernate SQL output
Hibernate: select assignment0_.id as id1_0_, assignment0_.end_date as end_date2_0_, assignment0_.lecturerId as lecturer3_0_, assignment0_.name as name4_0_, assignment0_.start_date as start_da5_0_, assignment0_.studentId as studentI6_0_, assignment0_2_.specification as specific1_2_, case when assignment0_1_.id is not null then 1 when assignment0_2_.id is not null then 2 when assignment0_.id is not null then 0 end as clazz_ from assignments assignment0_ left outer join quiz_assignments assignment0_1_ on assignment0_.id=assignment0_1_.id left outer join project_assignments assignment0_2_ on assignment0_.id=assignment0_2_.id
Hibernate: select quizquesti0_.id as id5_4_0_, quizquesti0_.quiz_id as quiz_id1_4_0_, quizquesti0_.quiz_id as quiz_id1_4_1_, quizquesti0_.answer as answer2_4_1_, quizquesti0_.id as id5_4_1_, quizquesti0_.choices as choices3_4_1_, quizquesti0_.question as question4_4_1_ from quiz_questions quizquesti0_ where quizquesti0_.id=?
Hibernate: select quizquesti0_.id as id5_4_0_, quizquesti0_.quiz_id as quiz_id1_4_0_, quizquesti0_.quiz_id as quiz_id1_4_1_, quizquesti0_.answer as answer2_4_1_, quizquesti0_.id as id5_4_1_, quizquesti0_.choices as choices3_4_1_, quizquesti0_.question as question4_4_1_ from quiz_questions quizquesti0_ where quizquesti0_.id=?
Hibernate: select quizquesti0_.id as id5_4_0_, quizquesti0_.quiz_id as quiz_id1_4_0_, quizquesti0_.quiz_id as quiz_id1_4_1_, quizquesti0_.answer as answer2_4_1_, quizquesti0_.id as id5_4_1_, quizquesti0_.choices as choices3_4_1_, quizquesti0_.question as question4_4_1_ from quiz_questions quizquesti0_ where quizquesti0_.id=?
Hibernate: select quizquesti0_.id as id5_4_0_, quizquesti0_.quiz_id as quiz_id1_4_0_, quizquesti0_.quiz_id as quiz_id1_4_1_, quizquesti0_.answer as answer2_4_1_, quizquesti0_.id as id5_4_1_, quizquesti0_.choices as choices3_4_1_, quizquesti0_.question as question4_4_1_ from quiz_questions quizquesti0_ where quizquesti0_.id=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into assignments (end_date, lecturerId, name, start_date, studentId, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into quiz_assignments (id) values (?)
I've spent a good bit of time trying to fix this, the funny thing is that it was working earlier. Anyways the parent class gets saved successfully but the list of child objects do not get saved whatsoever. I've tried changing the save(Assignment a)
method in AssignmentDAO
to use dao.persist(a)
but that didn't work either.
Any help will be greatly appreciated.