[英]Java Hibernate delete cascade
我正在嘗試從數據庫中刪除測驗項目。 測驗包含任務對象列表。
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;
}
}
我使用以下方法刪除測驗。
public void deleteQuiz(Quiz q) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
session.delete(q);
transaction.commit();
}
我的桌子上有一個包含3個任務的測驗。 現在,如果我嘗試刪除測驗,則只有最后一個任務從數據庫中刪除,其余2個仍然保留。
知道如何使用級聯或類似的解決方案刪除所有3個嗎?
我建議您考慮使用Spring Data JPA https://spring.io/guides/gs/accessing-data-jpa/ ,它將使您的生活變得更加輕松。
使用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<>();
}
您的任務將定義為
@Entity
@Table(name = "task")
public class Task {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
為Quiz實體創建一個存儲庫接口(可以將其視為DAO):
public interface QuizRepository extends JpaRepository<Quiz, Long> {
}
您可以將其注入其他Spring托管bean中並使用其方法。
...
@Autowired
private QuizRepository quizRepository;
public void deleteQuiz(Quiz quiz) {
quizRepository.delete(quiz);
}
...
將@EnableJpaRepositories批注添加到您的配置類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.