0

I want to get the value of the result in query.

This is my query / repository file:

@Query("select p.name, p.contact_no, p.email_address, p.time_submitted, p.date_due from Application p where p.application_number like ?1")
List<Application> getApplicationSummaryInfo(String application_number);

Service:

public List<Application> getApplicationSummaryInfo(String application_number);

ServiceImpl:

@Override
public List<Application> getApplicationSummaryInfo(String application_number) {
    return (List<Application>)applicationRepository.getApplicationSummaryInfo(application_number);
}

Controller:

List<Application> app_info = applicationService.getApplicationSummaryInfo(application_no);

I only know how to access this in jsp file using foreach. But I want to access the fields in my controller file.

I tried doing this:

List<Application> app_info = applicationService.getApplicationSummaryInfo(application_no);
System.out.println(app_info.get(0));

and I get this result:

[Ljava.lang.Object;@28bf8e6e

then I tried getting the exact "name" field value

List<Application> app_info = applicationService.getApplicationSummaryInfo(application_no);
System.out.println(app_info.get(0).getName());

but I get this error

java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class com.pckg_name.model.Application ([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; com.rtc_insurance.model.Application is in unnamed module of loader 'app')

really hope you could help me. Thank you!

trumanblack1025
  • 471
  • 2
  • 8
  • 19

2 Answers2

0

Might be this will help you.

  1. Create a constructor with the fields mentioned in the sql. (name,contact_no, email_address,time_submitted,date_due) in Application class.
  2. Specify it in query like "select new Application(name,contact_no, email_address,time_submitted,date_due) from Application p where p.application_number like ?1".
  3. You will get the result in the form of Application object so you can access the fields of that object.
Atul
  • 3,043
  • 27
  • 39
0

Since Application is an entity, instead of :

@Query("select p.name, p.contact_no, p.email_address, p.time_submitted, p.date_due from Application p where p.application_number like ?1")

spring-data-jpa has already a method for you:

List<Application> findByApplication_numberContaining(String str);

So, there is no need to create the query yourself. Just define the above method inside your repository interface.

After that first make sure that your repository method is working as desired. Then, if you want iterate throught the list inside a JSP page, this answer is for you.

Let me know if it fixed the problem.

Morteza
  • 642
  • 7
  • 17