StzEZ
StzEZ

Reputation: 69

Hibernate duplicate primary key on cascade insert with more than one entity (Unidirectional one-to-many)

I have a entity with a unidirectional @OneToMany as follows:

public class Parent {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "id_status")
    private Status status;

    @OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true)
    @JoinColumn(name = "childId")
    private List<Child> children = new ArrayList<>();

}

Which references this with the unidirectional @OneToMany (Last column of Parent entity):

public class Child{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long childId;

    private Date date;

    @Length(max = 255)
    private String description;

}

My problem is that on insert/update/delete of parent entity, any operation with the child entity fails. Main issues are:

org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PUBLIC.PRIMARY_KEY_DF ON PUBLIC.CHILD(CHILD_ID) VALUES ( /* 2 */ CAST(2 AS DECFLOAT) )"; SQL statement:
update child set child_id=? where child_id=? [23505-212]
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "CHILD_ID"; SQL statement:
update child set child_id=null where child_id=? [23502-212]

There are some other minor issues, but it's mainly this that is giving me a hard time, thanks in advance.

Upvotes: 0

Views: 413

Answers (1)

StzEZ
StzEZ

Reputation: 69

In the end i "solved" it by basically making it bidirectional

Upvotes: 1

Related Questions