How to access a related entity relationship in a ThymeLeaf HTML Template

I am using Spring Boot and ThymeLeaf to send an email. In the email template I need to retrieve the related product entity for a bottle however I keep getting an error when I try to retrieve it like this ${}. What is the correct way to get the name from the related product entity?


    <tr th:if="${#lists.isEmpty(bottles)}">
        <td colspan="2">Information not available</td>
    <tr th:each="bottle : ${bottles}">
        <td colspan="4"><span th:text="${}">&nbsp;</span></td>  <!-- trying to get the related product entity -->


public class WGService {

    public Long sendReorderRequestEmail(WGIO.ReorderBottles.ReorderRequest request) {
        List<BottleReordering> requestedBottles = request.getBottleReordering();
        List<BottleReordering> products = new ArrayList<>();
        if(!CollectionUtils.isEmpty(requestedBottles)) {
            User user = userRepository.findUserById(request.getUserId());
            requestedBottles.forEach(rb -> {
                BottleReordering bottle = new BottleReordering();
            mailService.sendReorderEmail(user, products);


    public class MailService extends EmailService {

        public void sendReorderEmail(User user, List<BottleReordering> bottles) {
            Context context = createDefaultContext(user);
            context.setVariable("bottles", bottles);
            sendEmail(toEmail, fromEmail, fromName, createSubject("Reorder request"), context, "reorder_request.html")

BottleReordering entity

public class BottleReordering implements Serializable {
    @Column(name = "product_id")
    private Long productId;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "product_id", nullable = false, updatable = false, insertable = false)
    private Product product;


Product entity

public class Product {

    @Column(name = "id", updatable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "products_id_seq")
    @SequenceGenerator(name="products_id_seq", sequenceName = "products_id_seq", initialValue = 100, allocationSize = 1)
    private Long id;

    private String name;

    @OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
    private List<BottleReordering> bottleReordering;


Edit: additional info

Here is the exception I am getting

Exception evaluating SpringEL expression: "" (template: "reorder_request.html"

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


public class WGController {
    @PostMapping(value = "/wg/order/bottles/{wgId}")
    public ResponseEntity<Void> reorderBottles(@Valid @RequestBody WGIO.ReorderBottles.ReorderRequest request, @PathVariable required = true) Long wgId) throws URISyntaxException {
        Long wgId = wGService.sendReorderRequestEmail(request);
        return ResponseEntity.created(new URI(wgId.toString())).build();

Read more here:

Content Attribution

This content was originally published by ghan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: