出现以下错误:
{"timestamp":1535929757444,"status":500,"error":"Internal Server Error","exception":"org.springframework.dao.DataIntegrityViolationException","message":"could not execute statement; SQL [n/a]; constraint [\"PRIMARY KEY ON PUBLIC.WT_TASK(TASK_ID)\"; SQL statement:\ninsert into wt_task (exercise_id, task_id) values (?, ?) [23505-196]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement","path":"/api/word-transformation/new"}
我能够创建一个从数据库中获取JSON的服务,但是我无法设法上传它们。
这是我的服务
public WordTransformationExercise save(WordTransformationExerciseRequest wtRequest) {
WordTransformationExercise wordTransformation = new WordTransformationExercise();
wordTransformation.setAuthorId(wtRequest.getAuthor_id());
List<WordTransformationTaskRequest> testP = wtRequest.getwt_task();
List<WordTransformation> thisIsIt = new ArrayList<WordTransformation>();
for(WordTransformationTaskRequest task : testP) {
WordTransformation send = new WordTransformation();
send.setBody(task.getBody());
send.setResult(task.getResult());
send.setWord(task.getWord());
send.setWordAtIndex(task.getWord_at_index());
thisIsIt.add(send);
}
wordTransformation.setwt_task(thisIsIt);
this.wtRepository.save(wordTransformation);
return wordTransformation;
}
这是我的实体:
@Entity
@Table(name = "wt_exercise")
public class WordTransformationExercise implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "author_id")
private Long authorId;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "wt_task",
joinColumns = @JoinColumn(name = "exercise_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "task_id", referencedColumnName = "task_id"))
private List<WordTransformation> wt_task;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getAuthorId() {
return authorId;
}
public void setAuthorId(Long authorId) {
this.authorId = authorId;
}
public void setwt_task(List<WordTransformation> list) {
this.wt_task = list;
}
public Collection<?> getwt_task() {
return this.wt_task;
}
}
这是任务实体:
@Entity
@Table(name = "wt_task")
public class WordTransformation implements Serializable {
@Id
@Column(name = "task_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long task_id;
@Column(name = "body")
private String body;
@Column(name = "result")
private String result;
@Column(name = "word")
private String word;
@Column(name = "word_at_index")
private Integer wordAtIndex;
@Column(name = "exercise_id")
private Long exercise_id;
public void setExercise_id(Long exercise_id) {
this.exercise_id = exercise_id;
}
public Long getExercise_id() {
return exercise_id;
}
public void setId(Long task_id) {
this.task_id = task_id;
}
public Long getId() {
return task_id;
}
public void setBody(String body) {
this.body = body;
}
public String getBody() {
return body;
}
public void setResult(String result) {
this.result = result;
}
public String getResult() {
return result;
}
public void setWord(String word) {
this.word = word;
}
public String getWord() {
return word;
}
public void setWordAtIndex(Integer wordAtIndex) {
this.wordAtIndex = wordAtIndex;
}
public Integer getWordAtIndex() {
return wordAtIndex;
}
}
当我将wt_task设置为null时,我的API会很好地返回'id'和'authorId'
这是我的sql的示例:
将wt_exercise(id,author_id)插入VALUES(2,2);
插入wt_task(正文,结果,单词,word_at_index,exercise_id,task_id)VALUES(“请开始”,“运行”,“运行”,13、2、2); >
答案 0 :(得分:0)
这取决于您要做什么。
如果要尝试更新,则应从存储库中检索它并设置值,而不是创建新对象并尝试使用现有键保存它。
如果您要尝试创建新内容,则根本不要设置密钥。