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 ${bottle.product.name}
. What is the correct way to get the name
from the related product
entity?
reorder_request.html
<tbody>
<tr th:if="${#lists.isEmpty(bottles)}">
<td colspan="2">Information not available</td>
</tr>
<tr th:each="bottle : ${bottles}">
<td colspan="4"><span th:text="${bottle.product.name}"> </span></td> <!-- trying to get the related product entity -->
</tr>
</tbody>
WGService
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();
bottle.setProductId(rb.getProductId());
products.add(bottle);
});
mailService.sendReorderEmail(user, products);
}
}
MailService
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;
@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "product_id", nullable = false, updatable = false, insertable = false)
private Product product;
}
Product entity
public class Product {
@Id
@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;
@Column(name="name")
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: "bottle.product.name" (template: "reorder_request.html"
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
Controller
public class WGController {
@Validated
@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: https://stackoverflow.com/questions/66269453/how-to-access-a-related-entity-relationship-in-a-thymeleaf-html-template
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.