Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

JPA fazendo update na FK

Olá senhores

Eu estava fazendo alguns testes, e me deparei com um problema

Reparei que quando faço um insert com CascadeType.PERSIST, ao inserir a filha o hibernate não insere a entidade filha já com o id do pai

Ele faz o insert e depois faz o update da FK, e isso me força a deixar o campo dept_id podendo ser null.

O framework trabalha realmente desse jeito ? Alguém sabe me informar ?

Relacionamentos

@Entity
@Data
public class Department {

    @Id
    @GeneratedValue
    private UUID id;
    private String name;

    @OneToMany(cascade={CascadeType.PERSIST})
    @JoinColumn(name = "dept_id")
    List<Employee> employees = new ArrayList<>();
}
@Entity
@Data
public class Employee {

    @Id
    @GeneratedValue
    private UUID id;
    private String name;
    private String email;
    private Date dateOfBirth;

    @ManyToOne()
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @JsonBackReference
    private Department department;
}

log

2020-10-06 01:42:03.993 DEBUG 11552 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    insert 
    into
        department
        (name, id) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        department
        (name, id) 
    values
        (?, ?)
2020-10-06 01:42:03.999 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [TI]
2020-10-06 01:42:03.999 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [OTHER] - [71d8a5fd-8097-469d-bfe3-9297fc862c6c]
2020-10-06 01:42:04.002 DEBUG 11552 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    insert 
    into
        employee
        (date_of_birth, dept_id, email, name, id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        employee
        (date_of_birth, dept_id, email, name, id) 
    values
        (?, ?, ?, ?, ?)
2020-10-06 01:42:04.003 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [Wed Mar 22 21:00:00 BRT 1995]
2020-10-06 01:42:04.003 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [OTHER] - [null]
2020-10-06 01:42:04.003 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [legend@spacer]
2020-10-06 01:42:04.003 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [Legend Thugueder]
2020-10-06 01:42:04.003 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [OTHER] - [842018ca-fea1-42a2-91a8-c4a3f422e8b1]
2020-10-06 01:42:04.007 DEBUG 11552 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    update
        employee 
    set
        dept_id=? 
    where
        id=?
Hibernate: 
    update
        employee 
    set
        dept_id=? 
    where
        id=?
2020-10-06 01:42:04.007 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [OTHER] - [71d8a5fd-8097-469d-bfe3-9297fc862c6c]
2020-10-06 01:42:04.007 TRACE 11552 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [OTHER] - [842018ca-fea1-42a2-91a8-c4a3f422e8b1]
2 respostas
solução!

É normal, o hibernate primeiro adiciona as entradas do relacionamento filho com o valor de relacionamento nulo e, em seguida, atualiza a entrada.

Entendi, vlws pela resposta Otávio

A única maneira de resolver isso, é não utilizar o insert cascade do hibernate ?

Porque deixar a fk podendo ser null no banco não é uma coisa muito boa.