繁体   English   中英

ORA-01400:使用Hibernate插入外键时出错

[英]ORA-01400: Error while inserting Foreign Key using Hibernate

我有两个表AB其中一个外键从B到一个名为FK_A的外键。 我对FK_A有一个非空约束。

我有以下两个类:

@Entity
@Table(name = "A")
public class A {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "ID")
    private long id;

    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name = "FK_A", nullable = true)
    private Set<B> b; 

    //getters setters etc.
}


@Entity
@Table(name = "B")
public class B {
    //attributes, getters setters etc.
}

读取工作正常但是当我尝试将A写入DB时,我得到:ORA-01400:无法插入NULL ("SCHEMA"."B"."FK_A")

我要插入实体的代码类似于:

@PersistenceContext(unitName = "ab")
private EntityManager em;

A a = new A();
B b = new B();

Set<B> bList = new HashSet();
bList.add(b);
a.setB(bList);

em.persist(a);

现在如果我是正确的,不应该根据它自动生成的id自动在表B填充FK_A 如果没有,我该如何设置呢?

看看mappedBy OneToMany.mappedBy() JavaDoc的:

The field that owns the relationship. Required unless the relationship is unidirectional.

它是Hibernate关键字“inverse”的同义词。

另见这个问题

另外,作为变体,您的类B可以使用@ManyToOne(optional = false)注释声明的A类属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM