java - 保存单向一对多映射时,Hibernate 在外键字段中插入空值

标签 java spring spring-boot hibernate jpa

我有一个单向的一对多关系。一方面是家长,另一方面是 child 。一个家长可以有很多 child 。但是对于一个 child 来说,只有一个 parent 。在 Java 方面,关系是单向的,我需要访问 PARENT 的 CHILDS,但我不想为 CHILDS 存储 PARENT。所以这些是对象:
家长:

@Entity
public class Parent {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private String job;
    
    @OneToMany(targetEntity = Child.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "PARENT_ID")
    private Set<Child> childs;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getJob() { return job; }
    public void setJob(String job) { this.job = job; }

    public Set<Child> getChilds() {
        if(childs != null) { return childs; }
        else {
            childs = new HashSet<Child>();
            return childs;
        }
    }
    public void setChilds(Set<Child> childs) { this.childs = childs; }

}
child :
@Entity
public class Child {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String hobby;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getHobby() { return hobby; }
    public void setHobby(String hobby) { this.hobby = hobby; }

}
这是创建一个 child 的代码,一个有该 child 的 parent ,然后保存 parent :
@Test
public void test() {
    Child c = new Child();
    c.setHobby("hobby");
    
    Parent p = new Parent();
    p.setJob("test");
    p.getChilds().add(c);
    
    parentRepository.save(p);
}
然后当我运行代码时出现错误,因为 Hibernate 在插入时没有在 CHILD 上设置 PARENT_ID。在日志中很明显,Hibernate 从序列生成器中检索了所需的两个 id,但它使 CHILD.PARENT_ID 为空:
2020-07-28 13:21:00.689  INFO 16295 --- [           main] jpa_hibernate_spring_boot.MyTest         : Starting MyTest on riskop-ESPRIMO-P556 with PID 16295 (started by riskop in /home/riskop/Documents/privat/java/jpa_hibernate_spring_boot)
2020-07-28 13:21:00.690  INFO 16295 --- [           main] jpa_hibernate_spring_boot.MyTest         : No active profile set, falling back to default profiles: default
2020-07-28 13:21:00.950  INFO 16295 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-07-28 13:21:00.988  INFO 16295 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 32ms. Found 1 JPA repository interfaces.
2020-07-28 13:21:01.362  INFO 16295 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-07-28 13:21:01.491  INFO 16295 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-07-28 13:21:01.608  INFO 16295 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-28 13:21:01.660  INFO 16295 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-07-28 13:21:01.703  INFO 16295 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.18.Final
2020-07-28 13:21:01.743  INFO 16295 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-07-28 13:21:01.820  INFO 16295 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-07-28 13:21:01.977  INFO 16295 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2020-07-28 13:21:02.388  INFO 16295 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-07-28 13:21:02.393  INFO 16295 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-07-28 13:21:02.521  INFO 16295 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-07-28 13:21:02.526  INFO 16295 --- [           main] jpa_hibernate_spring_boot.MyTest         : Started MyTest in 1.983 seconds (JVM running for 2.575)
2020-07-28 13:21:02.578 DEBUG 16295 --- [           main] org.hibernate.SQL                        : 
    call next value for hibernate_sequence
2020-07-28 13:21:02.595 DEBUG 16295 --- [           main] org.hibernate.SQL                        : 
    call next value for hibernate_sequence
2020-07-28 13:21:02.601 DEBUG 16295 --- [           main] org.hibernate.SQL                        : 
    insert 
    into
        parent
        (job, id) 
    values
        (?, ?)
2020-07-28 13:21:02.603 TRACE 16295 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [test]
2020-07-28 13:21:02.604 TRACE 16295 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]
2020-07-28 13:21:02.605 DEBUG 16295 --- [           main] org.hibernate.SQL                        : 
    insert 
    into
        child
        (hobby, id) 
    values
        (?, ?)
2020-07-28 13:21:02.606 TRACE 16295 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [hobby]
2020-07-28 13:21:02.606 TRACE 16295 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [2]
2020-07-28 13:21:02.607  WARN 16295 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
2020-07-28 13:21:02.607 ERROR 16295 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL not allowed for column "PARENT_ID"; SQL statement:
insert into child (hobby, id) values (?, ?) [23502-200]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.105 s <<< FAILURE! - in jpa_hibernate_spring_boot.MyTest
[ERROR] test  Time elapsed: 0.089 s  <<< ERROR!
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
        at jpa_hibernate_spring_boot.MyTest.test(MyTest.java:33)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
        at jpa_hibernate_spring_boot.MyTest.test(MyTest.java:33)
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: 
NULL not allowed for column "PARENT_ID"; SQL statement:
insert into child (hobby, id) values (?, ?) [23502-200]
        at jpa_hibernate_spring_boot.MyTest.test(MyTest.java:33)
我该如何解决?
请注意,如果我从 CHILD.PARENT_ID 中删除非空约束,则代码有效。但我显然需要那张支票。
整个代码在这里:
https://github.com/riskop/jpa_hibernate_problem_parent_id_is_not_filled_by_hibernate

感谢 jwpol 提供“nullable = false”信息!如果我将其应用于 Parent:
    @JoinColumn(name = "PARENT_ID", nullable = false)
    private Set<Child> childs;
然后它开始工作!
但我很好奇为什么 Hibernate 默认不这样做,为什么它会尝试更新 PARENT_ID 即使给出了“nullable = false”:
2020-07-28 14:16:02.161  INFO 20458 --- [           main] jpa_hibernate_spring_boot.MyTest         : Started MyTest in 2.007 seconds (JVM running for 2.599)
2020-07-28 14:16:02.220 DEBUG 20458 --- [           main] org.hibernate.SQL                        : 
    call next value for hibernate_sequence
2020-07-28 14:16:02.242 DEBUG 20458 --- [           main] org.hibernate.SQL                        : 
    call next value for hibernate_sequence
2020-07-28 14:16:02.250 DEBUG 20458 --- [           main] org.hibernate.SQL                        : 
    insert 
    into
        parent
        (job, id) 
    values
        (?, ?)
2020-07-28 14:16:02.252 TRACE 20458 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [test]
2020-07-28 14:16:02.253 TRACE 20458 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]
2020-07-28 14:16:02.255 DEBUG 20458 --- [           main] org.hibernate.SQL                        : 
    insert 
    into
        child
        (hobby, parent_id, id) 
    values
        (?, ?, ?)
2020-07-28 14:16:02.255 TRACE 20458 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [hobby]
2020-07-28 14:16:02.255 TRACE 20458 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]
2020-07-28 14:16:02.256 TRACE 20458 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [BIGINT] - [2]
2020-07-28 14:16:02.260 DEBUG 20458 --- [           main] org.hibernate.SQL                        : 
    update
        child 
    set
        parent_id=? 
    where
        id=?
2020-07-28 14:16:02.261 TRACE 20458 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2020-07-28 14:16:02.261 TRACE 20458 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [2]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.144 s - in jpa_hibernate_spring_boot.MyTest
您知道为什么会发生这种看似不必要的更新吗?

最佳答案

Vlad Mihalcea 有一篇关于您正在做什么的好文章。可以在 https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate 找到它.
基本上,当您提供@JoinColumn 注释时,Hibernate 将首先对父级执行持久化,然后将子级减去外键持久化,然后使用父级的主键更新子外键。这遵循 Hibernate 的刷新顺序。为了防止额外的更新,他的建议是使关联双向并通过父级的辅助方法双向管理关联。

关于java - 保存单向一对多映射时,Hibernate 在外键字段中插入空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63133393/

相关文章:

java - 尝试使用 logback 打印堆栈跟踪,但堆栈跟踪未打印

java - 我应该为我的新 Spring Batch 作业使用 Spring Data flow 服务器吗?

java - 使用“查看寻呼机”在选项卡中未显示列表

java - 当我尝试将配置类移动为测试类的内部类时,为什么会收到此错误消息?

java - 禁用 dbcp 连接池的重试

java - 从外部源(例如使用 Spring Boot 的 API)注入(inject)应用程序属性

java - 混合浮点和长计算会产生错误的答案,并且没有编译器警告

java - Osgi getService()

java - 从 GWT 项目引用 Eclipse Java 项目

java - 如何使用 Spring Data JPA 发出修改查询以通过其 ID 删除一组实体?