cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Encountered an error when attempting to retrieve data from the database and convert it to JSON.

manish19
Newcomer
0 Kudos
169

I'm tasked with retrieving 10 rows from a table (Item type) and its related tables in SAP Hybris, using the table name (Item type) as a parameter. For example, if the table name is "Product," I need to fetch 10 rows from the "Product" table along with its associated tables.

public List<ItemModel> getLatestItems(String tableName) { //tableName is dynamic
String queryString = String.format("SELECT {PK} FROM {%s} ORDER BY {creationtime} DESC", tableName);

FlexibleSearchQuery query = new FlexibleSearchQuery(queryString)error
query.setCount(10); // Limit to 10 rows

SearchResult<ItemModel> searchResult = flexibleSearchService.search(query);
return searchResult.getResult();
}

I can fetch the Data through flexible query but it gives the error "Infinite recursion" when conversion it to JSON using ObjectMapper

Note- tableName is dynamic.

Is there any way to solve this or any out of box feature to resolve this?

View Entire Topic
Anjali_Pathak
Associate
Associate
0 Kudos

Hi @manish19 ,

The "infinite recursion" error might occur due to bi-directional relationships in your entity classes.
For eg: if two entities reference each other, then the serialization process can run into an infinite loop.

Some approaches that you can try here are as follows:
1. You can use is try with @JsonIgnore annotation in one side of the entity to prevent it from being serialized.
2. You can leverage the usage of DTO conversion, instead of directly serializing the entities you can convert them into the DTOs (Data Trasnfer Object) that would only contain the required fields.
3. @JsonManagedReference on the parent entity side and @JsonBackReference on the child entity side may also handle bidirectional relationships.

Hope this helps! Have a great day ahead!

Thanks,
Anjali Pathak