JPA:删除@OneToMany关系级联时出现问题

时间:2016-07-13 12:33:01

标签: java hibernate jpa spring-data-jpa hibernate-onetomany

在删除具有 @OneToMany 关系的注册表时遇到问题。实体非常简单,虽然它们包括复合PK和继承:

@MappedSuperclass
public class MyTablePk {

    @Id
    @Column(name = "id1")
    private Integer id1;

    @Id
    @Column(name = "id2")
    private Integer id2;

    public MyTablePk() {
    }

    public MyTablePk(final Integer id1, final Integer id2) {
        this.id1 = id1;
        this.id2 = id2;
    }

    // getters and setters
    ...

}

@Entity
@Table(name = "my_table")
@IdClass(value = MyTablePk.class)
public class MyTable extends MyTablePk {

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @JoinColumns({ @JoinColumn(name = "id1", referencedColumnName = "id1"),
            @JoinColumn(name = "id2", referencedColumnName = "id2") })
    private List<MyTableTranslation> translations = new ArrayList<MyTableTranslation>();

    // getters and setters
    ...

}

@MappedSuperclass
public class MyTableTranslationPk {

    @Id
    @Column(name = "id1")
    private Integer id1;

    @Id
    @Column(name = "id2")
    private Integer id2;

    @Id
    @Column(name = "lang")
    private String lang;

    public MyTableTranslationPk() {
    }

    public MyTableTranslationPk(final Integer id1, final Integer id2, final String lang) {
        this.id1 = id1;
        this.id2 = id2;
        this.lang = lang;
    }

    // getters and setters
    ...

}

@Entity
@Table(name = "my_table_translation")
@IdClass(value = MyTableTranslationPk.class)
public class MyTableTranslation extends MyTableTranslationPk {

    @Column(name = "text")
    private String text;

    // getters and setters
    ...

}

当删除包含所有子元素的MyTable注册表时,JPA会神秘地尝试更新子代:

Hibernate: update my_table_translation set ID=null where ID=?
2016-07-13 10:23:24 ERROR SqlExceptionHelper:131 - ORA-01407: could not update ("PUI"."MY_TABLE_TRANSLATION"."ID") with a NULL value

为什么JPA会尝试更新这些已删除的注册表?

0 个答案:

没有答案