简体   繁体   中英

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. Now, if I try to delete the Quiz, only the last Task gets deleted from the DB, the other 2 remain.

Any idea how I can delete all 3 with a cascade or a similar solution?

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.

Using JPA, your Quiz class will look like this:

@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):

public interface QuizRepository extends JpaRepository<Quiz, Long> {
}

You can inject this into your other Spring managed beans and use its methods.

...
@Autowired
private QuizRepository quizRepository;

public void deleteQuiz(Quiz quiz) {
   quizRepository.delete(quiz);
}
...

Add the @EnableJpaRepositories annotation to your configuration class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM