[英]Java Hibernate delete cascade
I am trying to delete a Quiz item from my database. 我正在尝试从数据库中删除测验项目。 The Quiz contains a list of Task objects.
测验包含任务对象列表。
public class Quiz {
private SimpleStringProperty name = new SimpleStringProperty();
private int quiz_id;
private Teacher teacherOwner;
private List<Task> taskList;
public Quiz() {
}
public Quiz(String name, Teacher teacherOwner) {
this.name.set(name);
this.teacherOwner = teacherOwner;
taskList = new ArrayList<>();
}
public Quiz(String name, Teacher teacherOwner, List<Task> taskList) {
this.name.set(name);
this.teacherOwner = teacherOwner;
this.taskList = taskList;
}
@Column(nullable = false)
public String getName() {
return name.get();
}
public SimpleStringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
@OneToMany(targetEntity = Task.class, orphanRemoval = true, cascade = {javax.persistence.CascadeType.ALL})
public List<Task> getTaskList() {
return taskList;
}
public void setTaskList(List<Task> taskList) {
this.taskList = taskList;
}
@Id
@GenericGenerator(name="id" , strategy="increment")
@GeneratedValue(generator="id")
public int getQuiz_id() {
return quiz_id;
}
public void setQuiz_id(int quiz_id) {
this.quiz_id = quiz_id;
}
@ManyToOne(targetEntity = Teacher.class)
public Teacher getTeacherOwner() {
return teacherOwner;
}
public void setTeacherOwner(Teacher teacherOwner) {
this.teacherOwner = teacherOwner;
}
}
@Entity
public class Task {
private SimpleStringProperty question = new SimpleStringProperty();
private Solution answer1, answer2, answer3, answer4;
private int task_id;
public Task(String question, Solution answer1, Solution answer2, Solution answer3, Solution answer4) {
this.question.set(question);
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.answer4 = answer4;
}
public Task() {}
@Column(nullable = false)
public String getQuestion() {
return question.get();
}
public SimpleStringProperty questionProperty() {
return question;
}
public void setQuestion(String question) {
this.question.set(question);
}
@OneToOne(targetEntity = Solution.class, cascade = {CascadeType.ALL})
public Solution getAnswer1() {
return answer1;
}
public void setAnswer1(Solution answer1) {
this.answer1 = answer1;
}
@OneToOne(targetEntity = Solution.class, cascade = {CascadeType.ALL})
public Solution getAnswer2() {
return answer2;
}
public void setAnswer2(Solution answer2) {
this.answer2 = answer2;
}
@OneToOne(targetEntity = Solution.class, cascade = {CascadeType.ALL})
public Solution getAnswer3() {
return answer3;
}
public void setAnswer3(Solution answer3) {
this.answer3 = answer3;
}
@OneToOne(targetEntity = Solution.class, cascade = {CascadeType.ALL})
public Solution getAnswer4() {
return answer4;
}
public void setAnswer4(Solution answer4) {
this.answer4 = answer4;
}
@Id
@GenericGenerator(name="id" , strategy="increment")
@GeneratedValue(generator="id")
public int getTask_id() {
return task_id;
}
public void setTask_id(int task_id) {
this.task_id = task_id;
}
}
I delete the Quiz with the following method. 我使用以下方法删除测验。
public void deleteQuiz(Quiz q) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
session.delete(q);
transaction.commit();
}
I have a Quiz with 3 Tasks in my table. 我的桌子上有一个包含3个任务的测验。 Now, if I try to delete the Quiz, only the last Task gets deleted from the DB, the other 2 remain.
现在,如果我尝试删除测验,则只有最后一个任务从数据库中删除,其余2个仍然保留。
Any idea how I can delete all 3 with a cascade or a similar solution? 知道如何使用级联或类似的解决方案删除所有3个吗?
I would recommend that you look into using Spring Data JPA https://spring.io/guides/gs/accessing-data-jpa/ , it will make your life so much easier. 我建议您考虑使用Spring Data JPA https://spring.io/guides/gs/accessing-data-jpa/ ,它将使您的生活变得更加轻松。
Using JPA, your Quiz class will look like this: 使用JPA,您的Quiz类将如下所示:
@Entity
@Table(name="quiz")
public class Quiz {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Task> tasks = new ArrayList<>();
}
Your Task will be defined as 您的任务将定义为
@Entity
@Table(name = "task")
public class Task {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
Create a repository interface for the Quiz entity (think of this as a DAO): 为Quiz实体创建一个存储库接口(可以将其视为DAO):
public interface QuizRepository extends JpaRepository<Quiz, Long> {
}
You can inject this into your other Spring managed beans and use its methods. 您可以将其注入其他Spring托管bean中并使用其方法。
...
@Autowired
private QuizRepository quizRepository;
public void deleteQuiz(Quiz quiz) {
quizRepository.delete(quiz);
}
...
Add the @EnableJpaRepositories annotation to your configuration class. 将@EnableJpaRepositories批注添加到您的配置类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.