如何解决从孩子保存时,休眠一对多双向连接会提供新的父母
我正在尝试通过 freemarker 将子项添加到预先存在的父项中。但是每次尝试时,都会出现以下错误:
org.postgresql.util.PSQLException: ERROR: insert or update on table "CHILD TABLE" violates foreign key constraint "KEY" Detail: Key (id)=(WRONG NUMBER) is not present in table "PARENT TABLE".
我似乎每次都按顺序给出一个新的父 id,而不是在调用 childRepository.save(child);
之前给出并出现在子对象中的父 id。
代码片段:
家长:
@Entity
@Table(name = "parent")
@EntityListeners(AuditingEntityListener.class)
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToMany(mappedBy = "parent")
private Set<Child> children = new HashSet<Child>();
孩子:
@Entity
@Table(name = "child")
@EntityListeners(AuditingEntityListener.class)
public class Child{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
@JoinColumn(name = "parent_id",nullable = false)
private Parent parent;
Freemarker 控制器:
@Autowired
private ChildRepository childRepository;
@RequestMapping("/addchild/{id}")
public String addChild(Model model,@PathVariable(value = "id") Long parentId) {
String method = "addChild";
Utils.log(method,"started");
parent parent= parentRepository.findById(parentId)
.orElseThrow(() -> new IllegalArgumentException("parent " + parentId + " not found"));
Utils.log(method,"loaded " + parent);
child child = new child();
child.setparent(parent);
model.addAttribute("child",child);
return "child";
}
@PostMapping("/savechild")
public String savechild(Model model,child child) {
String method = "savechild";
Utils.log(method,"started");
child.getparent().addchild(child);
child = childRepository.save(child);
Utils.log(method,"saved " + child);
return "redirect:/parent/" + child.getparent().getId();
}
解决方法
我没有使用过 Freemarker,但 PostMapping 代码在我看来就像一个 Spring MVC 控制器,其中包含基于误解的错误代码。看, PostMapping 从例如获取一些输入一个 HTML 表单,并构造一个子实例,根据表单输入设置所有字段。
但由于 HTTP 是一种无状态协议,控制器方法绝对知道的只是您输入的内容,并且您从之前的 GET 调用中获得的父信息完全丢失了。因此,要在 POST 上添加子项,您还必须首先获取父项。
我本来希望看到类似的东西
@Autowired private ParentRepository parentRepository;
@Autowired private ChildrenRepository childrenRepository;
@RequestMapping("/parents/{parentId}/children/{childId}")
public String getChild(Model model,@PathVariable(value = "parentId") Long parentId,@PathVariable(value = "childId") Long childId) {
// Fetch a specific child for a specific parent fully determined by URL
Child child = childrenRepository.findByParentIdAndId(parentId,childId).orElseThrow(() -> new IllegalArgumentException("parent " + parentId + "child " + childId + " not found"));
model.addAttribute("child",child);
return "child";
}
// The parent to add to is referenced by its url,the child constructed by form data
@PostMapping("/parents/{id}")
public String saveChild(Model model,@PathVariable(value = "id") Long parentId,child child) {
// Fetch the parent you want to add a child to
parent parent = parentRepository.findById(parentId)
.orElseThrow(() -> new IllegalArgumentException("parent " + parentId + " not found"));
// Add the child constructed from form data and send to parent specific url
parent.addchild(child);
// Save parent with updated list of children
parentRepository.save(parent)
//
return "redirect:/parents/" + parent.getId();
}
,
错误是一个错误的约束,可能是早期版本代码的工件
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。