Home > Software design >  JSONObject().put using a List Throws recursivelyDefinedObjectException
JSONObject().put using a List Throws recursivelyDefinedObjectException

Time:02-09

I have a query that generates a List of Tickets using hibernate like so :

Query query = session.createQuery(
        "SELECT T.id, T.Objet, T.Details, T.Etat,T.Severity, T.createDateTime, T.user, T.Attachment, U.lastName, L.nomLogiciel, V.nomVersion,T.AssignedTo,T.ClosedBy,T.closedDateTime,T.assignedDateTime, U.firstName, U.username, U.Email, U.Phone  FROM Ticket T, User U, Logiciel L, Version V "
          "WHERE T.user= :user and T.user=U.user_id AND L.logiciel_id=T.Logiciel AND T.Version=V.version_id AND T.Etat!='fermer' ORDER BY T.createDateTime ")
        .setParameter("user", user);

I tried to see screenshot

What the hell is causing this ?! I checked my Entity definitions and they look alright to me.

Side note

My Ticket object contains the User object, which contains a list of the Users's Tickets, and it goes on and on forever, infinite recursion, is this normal ? are my Entity definitions wrong ? how can i fix this ?

CodePudding user response:

You can go with Map.

Map<String,Object> mapObject = new HashMap<>();
List allTickets = ticketDao.getTicketsByUserDao(user);
mapObject.put("ticket",allTickets);

CodePudding user response:

The Error :

The allTickets List contains an infinitely recursive User object, and therefore could not be parsed to a JSONObject.

What caused it ?

this query :

Query query = session.createQuery(
        "SELECT T.id, T.Objet, T.Details, T.Etat,T.Severity, T.createDateTime, T.user, T.Attachment, U.lastName, L.nomLogiciel, V.nomVersion,T.AssignedTo,T.ClosedBy,T.closedDateTime,T.assignedDateTime, U.firstName, U.username, U.Email, U.Phone  FROM Ticket T, User U, Logiciel L, Version V "
          "WHERE T.user= :user and T.user=U.user_id AND L.logiciel_id=T.Logiciel AND T.Version=V.version_id AND T.Etat!='fermer' ORDER BY T.createDateTime ")
        .setParameter("user", user);

Emphasis on T.user here :

There is no column called User in the Tickets table, instead there is user_id, so query.list() method returned an instance of an infinitely recusive User Object instead of the user_id which is an integer, * i think *.

The Fix

Change T.user to T.user_id.

The List can now be parsed to a JSONObject now with no errors.

  •  Tags:  
  • Related