Skip to content
Advertisement

how to define how many levels of objects to get in a JSON in java with jpa?

if we have a Product and Supplier object, where Product has a supplier and a supplier has a Product, how can we indicate to the entities that this is not an infinite cycle when obtaining the JSON of a product?

this would be something like

 {
      "name": "milk",
      "supplier": {
          "name": "supplier name",
          "products": [
               {
               "name": "milk",
                "supplier": {
                "products": [
                      "name": "milk",
                       "supplier": {
                        "products": [
                            "name": "milk",
                             "supplier": {
                               "products": [
                  
                  
                  ]
                  
                         ]
                  
                  ]
                },
           ]
       }
    }

how could we configure this in java with JPA by levels? so that it is not an infinite cycle, and that it only returns 3 layers or levels of this infinite cycle?

Advertisement

Answer

This has nothing to do with JPA but with your JSON serialization library. I guess you are using Jackson as that is the default in Spring. In that case, annotate the products collection field in your supplier entity with @JsonIgnore to avoid serializing the collection to JSON.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement