无法为 Entity 类型的实体自动生成 java.lang.Long 类型的 id - Mongodb 和 Spring Boot

BAK*_*him 4 spring mongodb

我正在尝试将 POST 方法发送到 mongoDB,但我得到 Cannot autogenerate id of type java.lang.Long for entity of type Entity

这是我的实体:

@Data
@AllArgsConstructor
@Document(collection = "TaxesLine")
public class TaxesligneEntity {



    @Id
    private Long taxesLigneId;
    private Date dateReception;
    private String label;
    private int nbrLignes;
    private int nbrOperationW;
    private int nbrOperationV;
    private BigDecimal initalSol;
    private BigDecimal finalSol;
    private List<LigneEntity> lignes;



    public TaxesligneEntity(){
        super();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 POST 方法:

{
  "taxesLigneId": 1200,
  "dateReception": "2022-03-04T01:17:59.344Z",
  "label": "5488",
  "nbrLignes": 541,
  "nbrOperationW": 4521,
  "nbrOperationV": 5421,
  "initalSol": 541,
  "finalSol": 0,
  "lignes": [
    {
      "lignesId": 54,
      "dateOperation": "2022-03-04T01:17:59.345Z",
      "operationNature": "string",
      "ert": "string",
      "numC": 0,
      "ammout": 0,
      "CD": 0,
      "rC": 0,
      "rF": 0,
      "paymentM": "string",
      "operationC": {
        "OperationD": "2022-03-04T01:17:59.345Z",
        "operationCh": {
          "numCh": 54,
          "cheq": {
            "numCh": 88,
            "acteur": {
              "nActeur": "string",
              "pActeur": "string"
            }
          }
        },
        "operationEs": {
          "pwa": "string",
          "nomEm": "string",
          "preEm": "string"
        },
        "operationV": {
          "pwa": "string",
          "compB": {
            "rcinb": "string",
            "Swift": "string",
            "ban": {
              "nomBan": "string"
            },
            "acteur": {
              "nAct": "string",
              "preAct": "string"
            }
          }
        },
        "produit": {
          "pId": 858,
          "pCode": "string",
          "pLabel": "string",
        }
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

{
    "timestamp": "2022-03-04T12:03:33.755+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Cannot autogenerate id of type java.lang.Long for entity of type rel.persistence.mongodb.entity.taxesLignesEntity!",
    "path": "/api/taxeslignes"
}
Run Code Online (Sandbox Code Playgroud)

我尝试将 String 和 Integer 作为它传递的 id,但字段为 null + 并非所有字段都保留在此处,这是更改为 String 和 Integer 的结果

"taxesLigneId": null,
      "dateReception": "null",
      "label": "0",
      "initalSol": 0,
      "finalSol": 0,
Run Code Online (Sandbox Code Playgroud)

小智 7

如果您没有根据本文使用字符串作为主键,那么使用 MongoDB 自动生成 Id 会很麻烦。如果您希望任何字段是唯一的,您可以随时在该字段上添加 @Indexed(unique=true) 注释。

查看本文中 spring boot 官方文档如何使用 MongoDB 创建 ids 。

A 还可以查看这篇文章中MongoDB官方文档如何使用spring boot创建id 。

他们都使用字符串作为 id 并不奇怪(我猜)。

@Data
@AllArgsConstructor
@Document(collection = "TaxesLine")
public class TaxesligneEntity {



    @Id
    private String id;
    @Indexed(unique=true)
    private Long taxesLigneId;
    private Date dateReception;
    private String label;
    private int nbrLignes;
    private int nbrOperationW;
    private int nbrOperationV;
    private BigDecimal initalSol;
    private BigDecimal finalSol;
    private List<LigneEntity> lignes;



    public TaxesligneEntity(){
        super();
    }}
Run Code Online (Sandbox Code Playgroud)

注意:也不要忘记更改您的存储库我想它现在应该看起来像这样

@存储库

公共接口 TaxesligneRepository 扩展 MongoRepository< TaxesligneEntity, String> { }