"Exception evaluating SpringEL expression" error while trying to iterate List(<Object>) in Thymeleaf Spring Boot

Rajeshwer :

I am new to spring boot and attempting to make a join between two unrelated entities (Transaction and Item) using JPA/JPQL. But I am getting the following error while using thymeleaf to display it in a table format:

2020-04-08 21:42:42.463 ERROR 73816 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "transactiondetail.tid" (template: "index" - line 27, col 21)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'tid' cannot be found on object of type 'java.lang.Object[]' - maybe not public or not valid?

Following is my thymeleaf implementation:

    <tr th:each="transactiondetail : ${listTransactionDetail}">
        <td th:text="${transactiondetail.tid}">TID</td>
        <td th:text="${transactiondetail.from_uid}">From UID</td>
        <td th:text="${transactiondetail.to_uid}">To UID</td>
        <td th:text="${transactiondetail.item_name}">Item Name</td>
        <td th:text="${transactiondetail.item_count}">Count</td>
        <td th:text="${transactiondetail.status}">Status</td>

Following is the controller:

@Autowired
QueryService queryservice; 

@RequestMapping("/")
public String viewHomePage(Model model) {
    List<TransactionDetail> listTransactionDetail = queryservice.JPQLQuery();
    model.addAttribute("listTransactionDetail", listTransactionDetail);
    return "index";
    }

I am able to get the result when I simply return queryservice.JPQLQuery(), which means the joining and listing is working. Following is the service the controller is calling:

@Service
public class QueryService implements IQueryService {

    @Autowired
    EntityManagerFactory emf;

    @Override
    public List<TransactionDetail> JPQLQuery()
    {
        EntityManager em = emf.createEntityManager();

        Query query = em.createQuery("Select s.tid,s.from_uid,s.to_uid,d.item_name,s.item_count,s.status from Transaction s inner join Item d on s.item_id=d.item_id");
        @SuppressWarnings("unchecked")
        List<TransactionDetail> tranlist = (List<TransactionDetail>)query.getResultList();
        em.close();
        return tranlist;

    }

}

Shown below is the TransactionDetail class which is taking in the joined entities. As you can see, I have both constructors and getters/setters with proper names. Still not able to display the details correctly.

public class TransactionDetail {
    private Long tid;
    private int from_uid;
    private int to_uid;
    private String item_name;
    private int item_count;
    private int status;

    public TransactionDetail() {
    }

    public TransactionDetail(Long tid, int from_uid, int to_uid, String item_name, int item_count, int status) {
        super();
        this.tid = tid;
        this.from_uid = from_uid;
        this.to_uid = to_uid;
        this.item_name = item_name;
        this.item_count = item_count;
        this.status = status;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {    
        this.tid = tid;    
    }

    public int getFrom_uid() {    
        return from_uid;    
    }    
    public void setFrom_uid(int from_uid) {    
        this.from_uid = from_uid;    
    }

    public int getTo_uid() {    
        return to_uid;    
    }    
    public void setTo_uid(int to_uid) {    
        this.to_uid = to_uid;    
    }   

    public int getItem_count() {    
        return item_count;    
    }    
    public void setItem_count(int item_count) {    
        this.item_count = item_count;    
    }

    public int getStatus() {    
        return status;    
    }    
    public void setStatus(int status) {    
        this.status = status;    
    }

    public String getItem_name() {
        return item_name;
    }

    public void setItem_name(String item_name) {
        this.item_name = item_name;
    }

}

I am guessing the problem has to do with the way I am saving the results from query.getResultList() into List<TransactionDetail>. So any idea how I can solve this issue? Thanks in advance!

Rajeshwer :

Turns out the issue was due my attempt to convert the result from the query (which was bits of 2 different entities) into a new class object. Hence, fixed it by changing the query to include creation of a new instance of the target object with the constructor call:

TypedQuery<TransactionDetail> query = em.createQuery("Select NEW package_name.TransactionDetail(s.tid,s.from_uid,s.to_uid,d.item_name,s.item_count,s.status) from Transaction s inner join Item d on s.item_id=d.item_id", TransactionDetail.class);
List<TransactionDetail> tranlist = query.getResultList();

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix "Exception evaluating SpringEL expression" error after submitting a variable Spring/Thymeleaf

Exception evaluating SpringEL expression, spring guru course

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "ServiceAmc.id" (results:30)

how to resolve this : Exception evaluating SpringEL expression: "#authorization.expression('isAuthenticated()')"?

Error while trying to Iterate over List of Strings

Exception evaluating SpringEL expression: "#dates.format(passation.datepassation, 'dd-MMM-yyyy')"

Post Object with nested List object in Spring-boot and Thymeleaf

how to bind list of object for sending post request in spring boot and thymeleaf

Spring Boot & Thymeleaf: list of links

Spring Boot & Thymeleaf: list of links

Spring Boot Thymeleaf Dropdown List

Spring thymeleaf doesn't iterate over list

How to iterate list of objects in spring thymeleaf

Unauthorized Error while Trying to Debug Spring Boot Rest Application

Issue with spring boot's thymeleaf auto configuration trying to resolve error template for rest application

Validation error while trying to parse a json array to List[Object] in Scala

Error while trying to iterate JSON array in JavaScript

Spring Boot Application - How can I pass data (list in list) from controller to Thymeleaf based on current object?

Thymeleaf - Iterate Object list based on an object's property

Getting error Cannot find a differ supporting object '[object Object]' of type 'object' while trying to populate list of objects

Spring Boot and Thymeleaf Neko HTML Error

Spring Boot Thymeleaf Whitelabel Error Page

Spring boot + Thymeleaf custom error messages

How to use spring mvc with thymeleaf to iterate over a list?

Spring Boot 404 when trying to load a HTML file using Thymeleaf

Thymeleaf + Spring Boot: Object creation with related object (Form & Return)

I'm getting error 404 while trying to access my spring boot app on Amazon Elastic Bean Stalk

Getting Tomcat 404 error while trying to deploy simple hello spring boot app

Error while trying to access my pgadmin 4 database using spring-boot maven and REST API in java