Jpa OneToMany关系不起作用

时间:2015-03-14 19:46:57

标签: exception jpa cascade check-constraints

我有一个问题试图用@OneToMany Relatioship持有一个实体,但我没有解决它。 (对不起,我不是英语母语人士)。

这些是实体:

@Entity
@Table(name = "PARENT")
@lombok.ToString
@lombok.EqualsAndHashCode(callSuper = false, of = { "id" })
public class Parent extends EntityBase<Long> {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PARENT_SEQ")
    @SequenceGenerator(name = "PARENT_SEQ", sequenceName = "PARENT_SEQ")
    @Column(name = "PARENT_ID")
    private Long id;

    @OneToMany(targetEntity = Child.class, cascade = CascadeType.ALL)
    @JoinColumns({ @JoinColumn(name = "PARENT_ID") })
    private List<Child> children;
}

儿童实体看起来像这样:

@Entity
@Table(name = "CHILD")
@lombok.ToString
@lombok.EqualsAndHashCode(callSuper = false, of = { "id" })
public class Child extends EntityBase<Long> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CHILD_SEQ")
    @SequenceGenerator(name = "CHILD_SEQ", sequenceName = "CHILD_SEQ")
    @Column(name = "CHILD_ID")
    private Long id;
}

我得到的例外是:

integrity constraint violation: NOT NULL check constraint; SYS_CT_10153 table: CHILD column: PARENT_ID

由于Cascade设置为all,但是,我会保留chidren,并且在Parent保持之后会发生这种情况。这样PARENT_ID已经存在,并将被设置为子行中的外键。

表格如下:

PARENT
PARENT_ID   NUMBER(30,0)    No  

CHILD
CHILD_ID    NUMBER(30,0)    No          
PARENT_ID   NUMBER(30,0)    No      

有人知道我可能做错了吗?提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我解决了。我忘了添加关系的另一面: - /。这意味着,child.setParent(parent)。抱歉浪费时间......