Java Hibernate从4迁移到5后,OneToOne关系会导致ConstraintViolationException

Java Hibernate从4迁移到5后,OneToOne关系会导致ConstraintViolationException,java,postgresql,hibernate,migration,Java,Postgresql,Hibernate,Migration,我们有这样的OneToOne双向关系: class Student: @PrimaryKeyJoinColumn @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @Fetch(FetchMode.JOIN) private StudentState state; public Student() { super(); state = new Stu

我们有这样的
OneToOne
双向关系:

class Student:

    @PrimaryKeyJoinColumn
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @Fetch(FetchMode.JOIN)
    private StudentState state;

    public Student() {
        super();
        state = new StudentState(this);
    }

    public StudentState getState() {
        return state;
    }

class StudentState:

    @MapsId
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "student")
    private Student student;

    @Column(name = "inactive")
    private Boolean isInactive = false;

    public StudentState() {
    }

    public StudentState(Student student) {
        this.student = student;
    }

    public boolean isInactive() {
        if (isInactive == null) {
            return false;
        }
        return isInactive;
    }

    public void setIsInactive(boolean isInactive) {
        this.isInactive = isInactive;
    }
下面是我们用来创建学生的代码:

Transaction t = session.beginTransaction();

Student student = new Student();
session.save(student);
student.getState().setIsInactive(true);

t.commit();
代码在Hibernate4中运行良好。 在Hibernate 5中,它与
org.Hibernate.exception.ConstraintViolationException:cannotexecute语句
错误

启用“showsql”表明,在事务提交时,hibernate尝试在
insert StudentState
query之前执行
insert Student
query。它导致:

Caused by: org.postgresql.util.PSQLException: 
ERROR: insert or update on table "studentstate" violates foreign key constraint "fk77j3mjaigcfotxlor35mdsl56"'
Detail: Key (student)=(12) is not present in table "students".

你知道它为什么会发生,为什么以前有效,以及如何修复它吗?TIA

问题的原因是hibernate内核中的一个bug。类org.hibernate.engine.spi.ActionQueue。 以下是修复后的代码:

            boolean hasAnyParentEntityNames(BatchIdentifier batchIdentifier) {
                return parentEntityNames.contains(batchIdentifier.getEntityName())
                        || parentEntityNames.contains(batchIdentifier.getRootEntityName());
            }

            boolean hasAnyChildEntityNames(BatchIdentifier batchIdentifier) {
                return childEntityNames.contains(batchIdentifier.getEntityName())
                        || childEntityNames.contains(batchIdentifier.getRootEntityName());
            }
以下是以前的代码:

        boolean hasAnyParentEntityNames(BatchIdentifier batchIdentifier) {
            return parentEntityNames.contains(batchIdentifier.getEntityName())
                    || parentEntityNames.contains(batchIdentifier.getRootEntityName());
        }

        boolean hasAnyChildEntityNames(BatchIdentifier batchIdentifier) {
            return childEntityNames.contains(batchIdentifier.getEntityName())
                    || parentEntityNames.contains(batchIdentifier.getRootEntityName());
        }

有人使用了错误的列表来查找子项,在某些情况下,该方法返回了相反的值。

您的DB模式是否发生了更改?听起来好像是在插入时检查约束,而不是在提交时检查约束……db模式是相同的。唯一被更改的是hibernate 4库被更改为hibernate 5以及依赖项(jpa、ehcache等)是否存在hibernate提前提交的方式?在hibernate中发现一个错误,该错误导致insert语句在某些情况下切换,而这些语句不应切换