如何将 ArrayList 发布到 Spring Boot H2 DB

Posted

技术标签:

【中文标题】如何将 ArrayList 发布到 Spring Boot H2 DB【英文标题】:How to POST ArrayList to spring boot H2 DB 【发布时间】:2018-09-02 23:20:29 【问题描述】:

我收到以下错误:

"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 示例:

INSERT INTO wt_exercise (id, author_id) VALUES (2, 2);

INSERT INTO wt_task(body, result, word, word_at_index, exercise_id, task_id) VALUES ('please start', 'running', 'run', 13, 2, 2);

【问题讨论】:

【参考方案1】:

这取决于你想做什么。

如果您尝试更新现有实体,则应从存储库中检索它并设置值,而不是创建新对象并尝试使用现有键保存它。

如果您尝试创建新事物,则根本不要设置密钥。

【讨论】:

我正在尝试创造一些新的东西。但我收到内部服务器错误。我不知道为什么

以上是关于如何将 ArrayList 发布到 Spring Boot H2 DB的主要内容,如果未能解决你的问题,请参考以下文章

Java - 如何将我的 ArrayList 写入文件,并将该文件读取(加载)到原始 ArrayList?

如何将 ArrayList 数据设置到 ListView?

如何将一个 ArrayList 的内容复制到另一个?

如何将arraylist数据存储到数据库中

如何将 Url 从 Firebase Storage 存储到 ArrayList 中?

如何将特定数据字段从 ArrayList 加载到某些 JTextFields?