【发布时间】:2020-03-22 19:01:00
【问题描述】:
我有一个 Parent 实体包含一个 Childs 列表。
- 父 A 包含子 A1
- 交易中
- 将 Child A2 添加到子列表中
- 保存(不刷新)
- 清除子列表(通过清除集合)
- 添加子 A3
- 保存并刷新
结果中:
- 数据库中有两条子记录:(A1被删除)
- 没有父信息的 A2 (==null)
- 带有家长信息的 A3
有人可以给我一份文件来说明 JPA 是如何工作的,而不是为 A2 添加父信息吗?
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "parent_id")
List<Child> children;
@Version
private Long recordVersion;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "parent_id", insertable = false, updatable = false)
private Parent parent;
@Version
private Long recordVersion;
public String toString() {
return "Child[id=" + id + ";name=" + name + "]";
}
}
@Transactional
public void addChild(Long parentId, String childName) {
Parent parent = getParent(parentId);
Child child = new Child();
child.setName("A2");
List<Child> children = Arrays.asList(child);
parent.getChildren().addAll(children);
parentRepository.save(parent);
parent = getParent(parentId);
parent.getChildren().clear();
Child child1 = new Child();
child1.setName("A3");
List<Child> spaces1 = Arrays.asList(child1);
parent.getChildren().addAll(spaces1);
parentRepository.saveAndFlush(parent);
}
更新 1: 如果我注释掉 parentRepository.save(parent);或者用 parentRepository.saveAndFlush(parent); 替换,数据库中只有 A3。
【问题讨论】:
-
你在 JPA 中使用什么下划线实现,如果是 Hibernate,Hibernate 中存在一个错误,导致你解释的行为hibernate.atlassian.net/browse/…
-
@MikhailChibel,我使用了 Hibernate。但是,我无法将错误映射到我的问题,您能否提供更多详细信息。谢谢。
标签: jpa spring-data