java - 保存单向一对多映射时,Hibernate 在外键字段中插入空值
问题描述
我有一个单向的一对多关系。一方面是父母,多方面是孩子。一个家长可以有多个孩子。但是对于一个孩子来说,只有一个父母。在 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; }
}
孩子:
@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_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 中删除 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 默认不这样做,为什么即使给出“nullable = false”它也会尝试更新 PARENT_ID:
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 的刷新顺序。为了防止额外的更新,他的建议是使关联双向并通过父级上的辅助方法双向管理关联。
推荐阅读
- javascript - 无法通过 aws-sdk Javascript 获取 Amazon Connect 的联系人属性
- r - 在ggplot scale_x_date中添加选定的日期(日和月)作为每年(每年相同)的x轴标签
- solr - SolrCloud MoreLikeThis 查询解析器错误
- javascript - 如何在 discord.js 中制作静音、取消静音、临时静音命令
- python - 在python中循环,不想从循环中得到相同的结果
- javascript - 无法在 HTMLAudioElement 处将属性“innerHTML”设置为 null。
- python - 无法使用 Selenium Python 访问登录弹出窗口元素
- java - Java 代码片段未按预期工作:使用 ArrayList
> - angular - d3.js 是否支持 IE 11 浏览器?我无法在 IE 浏览器上看到我的数据
- complex-numbers - 如何证明函数是全纯的?