使用休眠从数据库中删除项目

时间:2016-12-22 19:37:52

标签: java hibernate

我有一个表教师与表测验一起加入。 1老师可以有N个Quizes。

因此,我希望能够从数据库中删除教师。

from django.db.models.signals import pre_save
from django.dispatch import receiver
@receiver(pre_save, sender="Your model name")
def custom_signal(sender, update_fields, instance=None, **kwargs):
    # your algorithms

这些是教师和测验课程。我认为添加一个删除级联会解决它,但它没有。

教师

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails 

测验

@Entity
public class Teacher extends User{

    private List<Quiz> quizList;

    public Teacher() {
    }

    public Teacher(String name, String surname, String nick, String password) {
        super(name, surname, nick, password);
        quizList = new ArrayList<>();
    }

    @OneToMany
    @Cascade(value={CascadeType.REMOVE})
    @JoinColumn(name = "quiz_id")
    public List<Quiz> getQuizList() {
        return quizList;
    }

    public void setQuizList(List<Quiz> quizList) {
        this.quizList = quizList;
    }
}

知道如何解决它吗?我感谢所有的答案和建议。

编辑:嗯,我设法解决了,我在所有注释中都有点迷失。这是现在的工作版本。

教师

@Entity
public class Quiz {

    private SimpleStringProperty name = new SimpleStringProperty();

    private int quiz_id;

    private List<Task> taskList;

    private Teacher owner;

    public Quiz() {
    }

    public Quiz(String name, Teacher owner) {
        this.name.set(name);
        this.owner = owner;
        taskList = new ArrayList<>();
    }

    public Quiz(String name, Teacher owner, List<Task> taskList) {
        this.name.set(name);
        this.owner = owner;
        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);
    }

    @ManyToOne
    @JoinColumn(name = "teacher_id")
    public Teacher getOwner() {
        return owner;
    }

    public void setOwner(Teacher owner) {
        this.owner = owner;
    }

    @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;
    }

    @OneToMany
    public List<Task> getTasksList() {
        return taskList;
    }

    public void setTasksList(List<Task> taskList) {
        this.taskList = taskList;
    }
}

测验

@Entity
public class Teacher extends User{

    private List<Quiz> quizList;

    public Teacher() {
    }

    public Teacher(String name, String surname, String nick, String password) {
        super(name, surname, nick, password);
        quizList = new ArrayList<>();
    }

    @OneToMany(orphanRemoval = true)
    public List<Quiz> getQuizList() {
        return quizList;
    }

    public void setQuizList(List<Quiz> quizList) {
        this.quizList = quizList;
    }
}

1 个答案:

答案 0 :(得分:0)

父母:

@Entity
public class Teacher extends User{

    private List<Quiz> quizList;

    public Teacher() {
    }

    public Teacher(String name, String surname, String nick, String password) {
        super(name, surname, nick, password);
        quizList = new ArrayList<>();
    }

    @OneToMany(mappedBy = "teacher")
    @Cascade(CascadeType.ALL)
    public List<Quiz> getQuizList() {
        return quizList;
    }

    public void setQuizList(List<Quiz> quizList) {
        this.quizList = quizList;
    }
}

孩子:

@Entity
public class Quiz {

    private SimpleStringProperty name = new SimpleStringProperty();

    private int quiz_id;

    private List<Task> taskList;

    @ManyToOne
    @Cascade(CascadeType.ALL)
    @JoinColumn(name="teacher_id", nullable = false)
    private Teacher owner;

    public Quiz() {
    }

    public Quiz(String name, Teacher owner) {
        this.name.set(name);
        this.owner = owner;
        taskList = new ArrayList<>();
    }

    public Quiz(String name, Teacher owner, List<Task> taskList) {
        this.name.set(name);
        this.owner = owner;
        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);
    }

    public Teacher getOwner() {
        return owner;
    }

    public void setOwner(Teacher owner) {
        this.owner = owner;
    }

    @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;
    }

    @OneToMany
    public List<Task> getTasksList() {
        return taskList;
    }

    public void setTasksList(List<Task> taskList) {
        this.taskList = taskList;
    }
}

它可以帮助你。