【发布时间】:2021-11-27 12:27:39
【问题描述】:
我正在开发一个 Spring Boot 应用程序,我在其中使用 JPA 进行所有数据事务,并且在使用 OneToMany 关系时遇到了这个错误。
这是我的两个模型:
user.java
@Entity
public class user {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private String role;
@ManyToOne(cascade=CascadeType.ALL)
private file f;
@JsonIgnore
@OneToMany(targetEntity=job.class,cascade=CascadeType.ALL)
private List<job> applied;
//getters ,setters and constuctors.
}
job.java
@Entity
public class job {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String role;
private String company;
private String salary;
private String description;
private String level;
private Date expiry;
private String skills;
private String location;
@JsonIgnore
@OneToMany(targetEntity=user.class,cascade=CascadeType.ALL)
private List<user> candidates;
//getters setters and constructor
}
这里是触发错误的方法:
public String applyToJob(Integer userId,Integer jobId)
{
job jobToApply= jobRepo.getOne(jobId);
user applier=userRepo.getById(userId);
List<job> jobs=applier.getApplied();
jobs.add(jobToApply);
applier.setApplied(jobs);
userRepo.save(applier);
List<user> candidates=jobToApply.getCandidates();
System.out.println(candidates);
candidates.add(userRepo.getOne(userId));
System.out.println(candidates);
jobToApply.setCandidates(candidates);
jobRepo.save(jobToApply);
return "Job Apply Success";
}
现在我想在职位的候选人字段中添加数据,我收到以下错误:
2021-10-07 17:17:37.542 WARN 21696 --- [nio-8090-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1062, SQLState: 23000
2021-10-07 17:17:37.543 ERROR 21696 --- [nio-8090-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : Duplicate entry '2' for key 'job_candidates.UK_q0o76ghxl59c5ip3qwatsxo3f'
2021-10-07 17:17:37.543 INFO 21696 --- [nio-8090-exec-4] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
2021-10-07 17:17:37.564 ERROR 21696 --- [nio-8090-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [job_candidates.UK_q0o76ghxl59c5ip3qwatsxo3f]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '2' for key 'job_candidates.UK_q0o76ghxl59c5ip3qwatsxo3f'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.26.jar:8.0.26]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java
【问题讨论】:
-
您能分享您的数据库架构吗?是您创建的还是依靠 Hibernate 为您完成的?
-
JPA 为我做的
-
您是否在重试之前清除了数据库?
-
是的,我确实清除了数据库
-
从
userRepo.getOne(userId)返回的用户是否已经存在于候选集合中?该错误指示重复的候选者。在任何情况下,您都应该在将候选人添加到集合之前检查候选人集合中是否已经存在。
标签: java mysql spring spring-boot spring-data-jpa