springboot:将对象数组写入数据库

对单个表感兴趣


以下是我要存储在数据库中的JSON文件。我应该如何编写模型类?尝试了一个样本模型,但它仅存储对象的大小,而不存储实际数据。我也无法检索数据。

{
 sourceImageRepo: "xxx",sourcetagMatch: "xxx",targetProject: "xxx",targetFiles: [
  {
      name: "xxx",pattern: "xxx"
  },{
      name: "xxx",pattern: "xxx"
  }
 ]
}

我收到这样的错误消息:

{
    "timestamp": "2019-12-10T06:36:42.745+0000","status": 500,"error": "Internal Server Error","message": "Could not write JSON: (was java.lang.ArrayIndexOutOfBoundsException); nested exception is com.fasterxml.jackson.databind.JsonmappingException: (was java.lang.ArrayIndexOutOfBoundsException) (through reference chain: org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1[\"content\"]->com.example.demo.model.suiteInformations[\"targetFiles\"]->java.util.ArrayList[0])","path": "/suiteInformationses/1"
} 

当前模型

suiteInformations.java

package com.example.demo.model;

import javax.persistence.*;
import java.util.ArrayList;

@Entity
@Table(name = "suitesInformations")
public class suiteInformations {
    public suiteInformations(int id,String sourceImageRepo,String sourcetagMatch,String email,String targetProject,ArrayList<TargetFiles> targetFiles) {
        this.id = id;
        this.sourceImageRepo = sourceImageRepo;
        this.sourcetagMatch = sourcetagMatch;
        Email = email;
        this.targetProject = targetProject;
        this.targetFiles = targetFiles;
    }

    public suiteInformations() {}

    @Id
    @Column(name = "id")
    @GeneratedValue
    public int id;
    @Column(name = "sourceImageRepo")
    public String sourceImageRepo;
    @Column(name = "sourcetagMatch")
    public String sourcetagMatch;
    @Column(name = "Email")
    public String Email;
    @Column(name = "targetProject")
    public String targetProject;
    @Embedded
    public ArrayList<TargetFiles>  targetFiles;

    // getters/setters omitted

}

TargetFiles.java

package com.example.demo.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.ArrayList;

@Embeddable
public class TargetFiles {
    @Column(name = "name")
    public String name;

    @Column(name = "pattern")
    public String pattern;
}

数据库内容

SELECT * FROM SUITESINFORMATIONS;
ID EMAIL  SOURCEIMAGEREPO SOURCetaGMATCH    SIZE     TARGETPROJECT
1   xxx    xxx               xxx            1          xxx
super_1987cl 回答:springboot:将对象数组写入数据库

您缺少json中键的引号。我希望这只是一个错字。

{
 "sourceImageRepo": "xxx","sourceTagMatch": "xxx","targetProject": "xxx","targetFiles": [
  {
      "name": "xxx","pattern": "xxx"
  },{
      "name": "xxx","pattern": "xxx"
  }
 ]
}

您如何尝试将json映射到对象中? 它是api的请求主体吗?您能提供该信息吗?

,

我将使用Spring的spring-boot-starter-data-jpa依赖项。看看Spring的@Repository注释也可能对您有好处。

spring-boot-starter-data-jpa的示例教程:https://spring.io/guides/gs/accessing-data-jpa/

您的JSON文件可能会成为POJO类:

@Entity
public class SuiteInfo {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;

   private String sourceImageRepo;
   private String sourceTagMatch;
   private String targetProject;
   private List<SuiteFile> targetFiles;

   //relevant getters,setters,equals,etc

   public class SuiteFile {
      private String name;
      private String pattern;

      //relevant getters,etc
   }
}

然后可以创建存储库接口:

import org.springframework.data.repository.CrudRepository;

public interface SuiteInfoRepository extends CrudRepository<SuiteInfo,Long> {

  SuiteInfo findById(long id);

  // define other queries here
}

注意:(来自:https://spring.io/guides/gs/accessing-data-jpa/You need not write an implementation of the repository interface. Spring Data JPA creates an implementation when you run the application.

本文链接:https://www.f2er.com/2951415.html

大家都在问