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

huangapple go评论75阅读模式
英文:

Hibernate is inserting null value in foreign key field when saving a unidirectional one to many mapping

问题

以下是翻译好的部分:

问题描述:

我有一个单向的一对多关系。一侧是PARENT,多侧是CHILD。对于一个PARENT,可以有多个CHILD。但是对于一个CHILD,只有一个PARENT。在Java端,这个关系是单向的,我需要访问一个PARENT的所有CHILD,但我不想在CHILD中存储PARENT。以下是相关的对象:

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: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]

问题解决:

您应该如何修复它?

请注意,如果我从CHILD.PARENT_ID中删除了非空约束,那么代码可以正常工作。但显然我需要这个检查。

完整的代码在这里:

GitHub链接

感谢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.260 DEBUG 20458 --- [main] org.hibernate.SQL                        : 
    update
        child 
    set
        parent_id=? 
    where
        id=?

您有没有任何关于这个看似不必要的更新为什么会发生的想法?

英文:

I have a unidirectional one to many relationship. The one side is PARENT, the many side is CHILD. For one PARENT there can be many CHILD. But for a CHILD there is exactly one PARENT. On the Java side the relation is unidirectional, I need to access the CHILDS of a PARENT, but I don't want to store the PARENT for CHILDS. So these are the objects:

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 = &quot;PARENT_ID&quot;)
    private Set&lt;Child&gt; 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&lt;Child&gt; getChilds() {
        if(childs != null) { return childs; }
        else {
            childs = new HashSet&lt;Child&gt;();
            return childs;
        }
    }
    public void setChilds(Set&lt;Child&gt; 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; }

}

This is the code which creates a child, a parent with that child and then saves parent:

@Test
public void test() {
    Child c = new Child();
    c.setHobby(&quot;hobby&quot;);
    
    Parent p = new Parent();
    p.setJob(&quot;test&quot;);
    p.getChilds().add(c);
    
    parentRepository.save(p);
}

Then when I run the code there is an error because Hibernate does not set the PARENT_ID on CHILD when inserting it. In the log it is clear that Hibernate retrieved the two ids needed from the sequence generator YET it leaves CHILD.PARENT_ID null:

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 &#39;applicationTaskExecutor&#39;
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 &#39;default&#39;
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 &quot;PARENT_ID&quot;; 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 &lt;&lt;&lt; FAILURE! - in jpa_hibernate_spring_boot.MyTest
[ERROR] test  Time elapsed: 0.089 s  &lt;&lt;&lt; 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 &quot;PARENT_ID&quot;; SQL statement:
insert into child (hobby, id) values (?, ?) [23502-200]
        at jpa_hibernate_spring_boot.MyTest.test(MyTest.java:33)

How should I fix it?

Note that if I remove the not null constraint from CHILD.PARENT_ID, then the code works. But I obviously need that check.

The whole code is here:

https://github.com/riskop/jpa_hibernate_problem_parent_id_is_not_filled_by_hibernate


Thank you jwpol for the "nullable = false" info! If I apply that to Parent:

    @JoinColumn(name = &quot;PARENT_ID&quot;, nullable = false)
    private Set&lt;Child&gt; childs;

Then it starts working!

Yet I am curious why Hibernate does not do this by default and why does it try to update PARENT_ID even if "nullable = false" is given:

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

Do you have any idea why this seemingly unneccessary update happens?

答案1

得分: 8

Vlad Mihalcea在他的文章中详细讲解了你正在做的事情。你可以在https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate找到这篇文章。

基本上,当你提供@JoinColumn注解时,Hibernate会首先对父对象执行持久化操作,然后持久化子对象,不包括外键,最后更新子对象的外键以使用父对象的主键。这遵循了Hibernate的刷新顺序。为了防止额外的更新,他建议使关联是双向的,并通过父对象上的辅助方法双向管理关联。

英文:

Vlad Mihalcea has a good article on exactly what your are doing. It can be found at https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate.

Basically when you provide the @JoinColumn annotation Hibernate will perform a persist on the parent first, will persist the children minus foreign key second, and then update the child foreign keys with the parent's primary key. This follows Hibernate's flush order. To prevent the additional update his suggestion is to make the association bi-directional and manage the association bidirectionally via helper methods on the parent.

答案2

得分: 3

使用@JoinColumn(name = "PARENT_ID", nullable = false),你将告诉Hibernate不要有非空约束。

英文:

Use
@JoinColumn(name = &quot;PARENT_ID&quot;, nullable = false), you will tell hibernate that there is not null constraint.

huangapple
  • 本文由 发表于 2020年7月28日 19:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63133393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定