Search code examples
springspring-bootthymeleaf

Thymeleaf - Accessing nested object (new)


I'm new to thymeleaf, and have a little problem causing headache.

I have an entity "Payment", and Payment has "Manager" entity as attribute.

@ManyToOne
@JoinColumn(name = "manager")
private Manager manager;

And Manager Entity has attributes like id, name, .. so on. I want to access to the 'name' attribute from Payment Dto, like below.

<td><span th:text="${payment.manager.id}"></span></td>

However, it generates error code.

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null

I tried <td><span th:text="${payment.manager}"></span></td> this, and the program was running without error, although nothing was shown in that line.

How can I access to inner attribute(ex. name) of a nested objects like

Payment { Manager { id, name, ... } }

this?


Solution

  • First try to run the API on postman and check whether the data is coming right means

    payment :{
       id: "",
       .
       .
       "manager":{
          "name":"",
          .
          .
          .
        }
    }
    

    If not then there is problem while saving the entity payment. You saved the payment correctly but may be you didn't set the entity manager in the payment entity using ManyToOne relationship. Like this

    payment.setManager(manager); 
    

    Maybe you did an error in saving that.