Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot autogenerate id of type java.lang.Long for entity of type Entity - Mongodb and Spring Boot

Tags:

spring

mongodb

I'm trying to send a POST method to mongoDB but I get Cannot autogenerate id of type java.lang.Long for entity of type Entity

Here is my 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();
    }
}

Here is my POST method:

{
  "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",
        }
      }
    }
  ]
}

This is the error:

{
    "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"
}

I tried String and Integer as an id it passes but fields are null + not all fields persisted here is the result of changing to String and Integer

"taxesLigneId": null,
      "dateReception": "null",
      "label": "0",
      "initalSol": 0,
      "finalSol": 0,
like image 709
BAKHALED Ibrahim Avatar asked Nov 14 '25 09:11

BAKHALED Ibrahim


1 Answers

Autogenerating Ids using MongoDB is a hustle if you are not using a String as your primary key according to this article. If you want any field to be unique you can always add a @Indexed(unique=true) annotation on that field.

Check how spring boot official documentation uses to create ids using MongoDB in this article.

A also Check how MongoDB official documentation uses to create ids using spring boot in this article.

It's not by surprise that they all use Strings for their ids (I guess).

@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();
    }}

NB: Don't forget to change your repository too I guess it should now look like this

@Repository

public interface TaxesligneRepository extends MongoRepository< TaxesligneEntity, String> { }

like image 69
Godwin Tusime Avatar answered Nov 17 '25 08:11

Godwin Tusime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!