ORA-01400: error while inserting foreign key using Hibernate

I have two tables A

and one B

with a foreign key from B

to callable FK_A

. I have a non-null constraint on FK_A

.

I have the following two classes:

@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.
}

      

Reading works fine, but when I try to write A to the DB I get: ORA-01400: cannot insert NULL into ("SCHEMA"."B"."FK_A")

The code I'm trying to paste looks something like this:

@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);

      

Now, if I am not supposed to hibernate correctly FK_A

in the B

based table automatically id

, it was autogenerated. If not, how can I customize it?

+3


source to share


1 answer


Take a look at mappedBy OneToMany.mappedBy()

. JavaDoc:

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

      

This is a synonym for the Hibernate keyword "reverse".



See also this question .

Also, as an option, your class B may have a property for class A, declared with annotation @ManyToOne(optional = false)

.

0


source







All Articles