当前位置: 首页 > 知识库问答 >
问题:

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

阳光辉
2023-03-14

我有一个单向的一对多关系。一方是父母,多方是孩子。对于一个父母,可以有很多孩子。但是对于一个孩子来说,只有一个父母。在Java方面,关系是单向的,我需要访问父母的孩子,但是我不想为孩子存储父母。所以这些是对象:

起源:

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

}

孩子:

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

}

这是一段代码,用于创建一个子项、一个具有该子项的父项,然后保存父项:

@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\u ID。在日志中可以清楚地看到,Hibernate从序列生成器中检索到了所需的两个ID,但它留下了子项。父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 '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中删除not null约束,那么代码就可以工作了。但是我显然需要检查。

整个代码如下:

https://github.com/riskop/jpa_hibernate_problem_parent_id_is_not_filled_by_hibernate

感谢jwpol提供的“nullable=false”信息!如果我将其应用于家长:

    @JoinColumn(name = "PARENT_ID", nullable = false)
    private Set<Child> childs;

然后它开始工作!

然而,我很好奇为什么Hibernate在默认情况下不这样做,为什么它会尝试更新PARENT\u 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

你知道为什么会发生这种看似不必要的更新吗?

共有2个答案

谷梁玺
2023-03-14

使用@JoinCol列(name="PARENT_ID", nullable=false),你会告诉hibernate没有null约束。

符畅
2023-03-14

弗拉德·米哈尔恰(VladMihalcea)有一篇关于你正在做什么的好文章。可在以下网址找到https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate.

基本上,当您提供@JoinCol列注释时,Hibernate将首先对父节点执行持久化,其次将持久化子节点减去外键,然后使用父节点的主键更新子节点外键。这遵循Hibernate的刷新顺序。为了防止额外的更新,他的建议是使关联双向化,并通过父节点上的助手方法双向管理关联。

 类似资料:
  • 我有一对一关系的问题-保存后,外键总是被设置为空,即使我有关联的对象。我正在使用Hibernate4.3。7.最终版本为Spring 4.0。0.0版本和MySql 5.6数据库。 在我保存LawFirmProfile对象列后的情况下,参考lawFirm的lawFirm_id始终为空。 实体: 以下是负责保存此记录的代码: Hibernate和Spring配置: 道也是Spring豆,方法注释为@

  • 我在Parent中提供了一个简单的oneToMany关系,并在Chile Entity类中提供了相应的ManyToOne: 起源: } 儿童班级: 操作类: 示例案例:Status_code Reason_text abc abc1 xyz xyz1 保存表单:Status_code Reason_text abc abc1 xyz xyz1 abc abc1 xy z xyz2 对于任何操作:N

  • 我试图在Hibernate中的两个表之间创建一对一的单向主键关系。我使用xml Maaping,下面是我的java POJO类和它们各自的。hbm文件。 储物柜POJO类 locker.hbm.xml 下面是我用来保存student和locker对象的主类。

  • 我试图用Hibernate保存一个表。父表与其子表具有一对多关系。父表POJO有其子表POJO的集合。 当我保存父表时,数据也被插入到它的子表中,但它的外键不为NULL。 下面是我的代码:家长: 儿童: Hibernate服务: 我找到了一个解决方案,将我的孩子反向映射到父母,如下所示 但我在想这是否可以避免。我希望还有其他方法,我不需要把我的每个孩子都映射到父母那里。 Kinldy帮助,如果你能

  • b)对Employee类中的ReferencedColumnName='department id'使用@ManyToOne和@JoinColumn。 建议采用哪种方法?还是这两种方法用于完全不同的问题?

  • 我正在尝试我们非主键作为外键在我的应用程序。场景如下:我有EMPLOYEE和EMPLOYEE_PROPERTIES表。Employee和Employee属性之间存在一对多的关系。下面是我的架构: 下面是我的hibernate映射XML:------------------------------- -------------员工属性------------------- 是否可以引用非主键作为外键