Hibernate不保存子项列表,但即使使用Cascade也只保存父项

时间:2017-10-20 04:06:14

标签: java hibernate jpa hibernate-mapping

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输出

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

我花了很多时间来解决这个问题,有趣的是它早先工作了。无论如何,父类成功保存,但子对象列表不会被保存。我尝试更改save(Assignment a)中的AssignmentDAO方法以使用dao.persist(a),但这也不起作用。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

保存后尝试冲洗。

@Override
public void save(Assignment entity) {
    getCurrentSession().save(entity);
    getCurrentSession().flush();
}

我认为这是因为您使用在事务外执行的save()方法。你最好使用类似于save(with transaction)的persist(),并将实体对象添加到持久化上下文中。

@Override
public void save(Assignment entity) {
    getCurrentSession().persist(entity);
}