0

Am trying to get data from 3 tables and every time I end up getting an error

Ljava.lang.Object; cannot be cast to .model.ISECO at java.util.ArrayList.forEach

This are my entities

@Entity
public class IS01 {
    private String IEA;
    private String INUM;
    private String ILINE;
    private String I0103;

@Entity
public class ISOVER {
    private String IEA;
    private String ILINE;
    private String INUM;
    private String IRESULT;
    private String ICON;
    private String IBCON;
    private String CASE;
    private String RPTID

@Entity
public class POSTCO {
    private String CEA;
    private String CNUM;
    private String CLINE;
    private String PSCONTACT;

And this is my Repository

public interface LineSummary extends CrudRepository<ISOVER , String> {

    @Query("select c.ILINE , c.IRESULT,e.PSCONTACT, \n" +
            "c.ICON,c.IBCON, c.RPTID, c.CASE, d.i0103 as age\n" +
            "FROM ISOVER c \n" +
            "inner join IS01 d \n" +
            "on c.IEA = d.IEA and c.INUM = d.INUM and c.ILINE = d.ILINE\n" +
            "inner join POSTCO e on d.IEA = e.CEA and d.INUM = e.CNUM and d.ILINE = e.CLINE\n" +
            "where c.CASE like %?1%")
     Iterable<ISOVER> findEntriesByUserId(@Param("Case") String Case);

And this is my service

public ResponseEntity<Map<String, Object>> retrieveLineListingSQL(String Case){

    Iterable <ISOVER > stud = lineSummary.findEntriesByUserId(Case);


    Map<String, Object> parents = new HashMap<>();
    parents.put("totalMembers", 9);

    parents.put("questionaryinfo", new ArrayList<HashMap<String,Object>>());
    ArrayList<HashMap<String,Object>> listings = (ArrayList<HashMap<String,Object>>) parents.get("questionaryinfo");
    if (stud != null) {
        stud.forEach(d -> {
            HashMap<String,Object> entry = new HashMap<>();

            entry.put("adultquestionary","Yes");
            entry.put("caseNumber", d.getCASE());
            listings.add(entry);
        });
    }


    parents.put("DMStatus", "No review");
    parents.put("ages",  new HashMap<String, Object>());
    return ResponseEntity.ok().body(parents);
}

How can I return the results from the query and map them accordingly?

1 Answer 1

1

I believe this is your culprit:

if (stud != null) {
    stud.forEach(d -> {
        HashMap<String, Object> entry = new HashMap<>(); // < -- here

        entry.put("adultquestionary","Yes");
        entry.put("caseNumber", d.getCASE());
        listings.add(entry);
    });
}

Have your tried using *.model.ISECO instead of java.lang.Object? Does that work, any particular limitation?

Additionally, you could refactor you code to something way more simple, if you follow the same explanation provided in here: How to make nested JSON response with Array from a Stored procedure

  1. Create a response model that outputs the format you expect as response.
  2. There is no need for you to do all that collections handling one-by-one. The representation of an object in JSON is a MAP, basically let the Jackson JSON library do all that work for you.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.