gpt4 book ai didi

java - 映射错误 : More than one row with the given identifier was found

转载 作者:行者123 更新时间:2023-12-05 07:41:04 30 4
gpt4 key购买 nike

我正在开发一个 spring boot 应用程序,我正在使用 JPA 和 hibernate。
我遇到了两个实体 User 之间的映射关系问题评价
主要思想是通过另一个用户(评估者)评估一个用户

用户.java

public class User implements Serializable {

private static final long serialVersionUID = 1209816669845063949L;
@Id
@GeneratedValue
private int idUser;
private String login;
private String password;
@ManyToMany(mappedBy = "listUsers")
private List<ProjectGroup> listProjectGroup;

@OneToOne(mappedBy = "EvaluatedUser")
private Evaluation evaluation1;
@OneToOne(mappedBy = "Evaluater")
private Evaluation evaluation;
}

Evaluation.java

public class Evaluation {

@Id
@GeneratedValue
private int idEvaluation;
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "evaluateduser_fk")
private User EvaluatedUser;
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "evaluater_fk")
private User Evaluater;
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "project_fk")
private Project project;

@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "notes_fk")
private Notes notes;
}

EvaluationController.java
我正在使用此方法返回所有评估

@GetMapping(path = "all-evaluation", produces = {"application/json" })
public ResponseEntity<List<Evaluation>> getAllEvaluations() {
List<Evaluation> list = evaluationService.findAll();
return new ResponseEntity<List<Evaluation>>(list, HttpStatus.OK);
}

问题是,我无法对同一用户进行两次评估,我认为问题出在我的关系 OneToOne
这是我在返回 json 时遇到的异常:

There was an unexpected error (type=Internal Server Error, status=500). More than one row with the given identifier was found: 1, for class: com.talan.entity.Evaluation; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.talan.entity.Evaluation

任何帮助将不胜感激

最佳答案

当保存多行但 hibernate 只需要一行时会发生此错误。在您的情况下,正如您正确写的那样,OneToOne 不是与 choose 的正确关系,因为这样一来,每个 Evaluater/EvaluatedUser 只能有一个 Evaluation

您希望每个用户都能够多次成为 EvaluatedUser 和 Evaluater。每个评估只有一个用户作为 EvaluatedUser 和一个用户作为 Evaluater。因此,您必须将您的用户实体更改为

@OneToMany(mappedBy = "EvaluatedUser")
private Set<Evaluation> evaluationsEvaluated;
@OneToMany(mappedBy = "Evaluater")
private Set<Evaluation> evaluationsEvaluater;

和您的评估实体

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "evaluateduser_fk")
private User EvaluatedUser;
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "evaluater_fk")
private User Evaluater;

关于java - 映射错误 : More than one row with the given identifier was found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45638549/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com