JPA / Hibernate:反向OneToOne孤立删除约束违规

时间:2018-05-08 14:26:28

标签: hibernate jpa constraints one-to-one orphan

问题:

我在父母和孩子之间有 @OneToOne 关系,关系是由孩子映射,orphanRemoval 设置为true。孩子与第三个实体也有@ManyToMany关系。

将子项设置为null(希望删除子项)我得到完整性约束违规:外键...

测试模型:

导师学生具有OneToOne关系,与课程实体具有ManyToMany关系:

  • OneToOne关系由Student.tutor 映射(STUDENT表中的ID_TUTOR外键列)
    • cascade = ALL
    • orphanRemoval = true
  • ManyToMany关系保存在连接表STUDENT_COURSE中,其中包含STUDENT和COURSE表的外键。

我使用的是Hibernate 5.0.11(JPA 2.1)。对于我的测试,我使用的是HSQLDB,但问题也发生在Oracle数据库上。

情景:

我有一个导师链接到学生,其中包含课程列表。当将Tutor的Student设置为null Hibernate时,直接从Student发出SQL" delete,其中id =?"导致"完整性约束违规:外键无动作; FK1XM2HEI9CHMWOQF2WFM104NMG表:STUDENT_COURSE"

当OneToMany关系不是"反转时,不会发生此问题" (TUTOR表中没有 mappedBy 和ID_STUDENT外键)。在这种情况下,学生实体将从数据库中正确删除,并在STUDENT_COURSE表中删除相关记录。

代码:

@Entity
public class Tutor {
    private Long id;
    private String name;
    private Student student;
    public Tutor() { super(); }
    public Tutor(String name, Student student) {
        super();
        this.name = name;
        setStudent(student);
    }
    @Id
    @GeneratedValue
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    @OneToOne(cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="tutor")
    public Student getStudent() { return student; }
    public void setStudent(Student student) {
        this.student = student;
        if(student != null) {
            student.setTutor(this);
        }
    }
}

@Entity
public class Student {
    private Long id;
    private String name;
    private Map<String, Course> courses;
    private Tutor tutor;
    public Student() { super(); }
    public Student(String name, Map<String, Course> courses) {
        super();
        this.name = name;
        this.courses = courses;
    }
    @Id
    @GeneratedValue
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    @ManyToMany
    public Map<String, Course> getCourses() { return courses; }
    public void setCourses(Map<String, Course> courses) { this.courses = courses; }
    @OneToOne
    public Tutor getTutor() { return tutor; }
    public void setTutor(Tutor tutor) { this.tutor = tutor; }   
}

@Entity
public class Course {
    private Long id;
    private String name;
    public Course() { super(); }
    public Course(String name) {
        super();
        this.name = name;
    }
    @Id
    @GeneratedValue
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

public class CheckManyToManyDeletation {
    private static final Logger logger = Logger.getLogger(CheckManyToManyDeletation.class);
    private static SessionFactory sessionFactory;
    public static void main(String[] args) {
        DOMConfigurator.configure("log4j-config.xml");
        Class[] mappings =  new Class[] {Tutor.class, Student.class, Course.class};
        DBUtils.createTables(mappings);
        sessionFactory = DBUtils.createSessionFactory(mappings);
        try {
            long tutorId = initialImport();

            Session session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();

            Tutor tutor = session.get(Tutor.class, tutorId);
            logger.info("DeletingStudent");
            tutor.setStudent(null);

            tx.commit();
        } finally {
            sessionFactory.close();
        }

    }

    private static long initialImport() {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Student student = new Student();
        student.setName("student");

        Map<String, Course> courses = new HashMap<>();
        student.setCourses(courses);
        for (int i = 0; i < 5; i++) {
            Course course = new Course();
            course.setName("course" + i);
            session.save(course);
            courses.put(course.getName(), course);
        }

        Tutor tutor = new Tutor("tutor", student);
        session.save(tutor);
        tx.commit();
        session.close();
        return tutor.getId();
    }
}

说明:

实际上我们的应用程序带有一个庞大的数据模型:

  • 通过级联来保持和删除实体
  • 我们反转OneToOne关系以给孩子提供与其父母相同的身份证(通过@MapsId)。

0 个答案:

没有答案