[英]Accessing a one to many relationship
我正在使用Spring Boot
和ThymeLeaf
發送 email。 在 email 模板中,我需要檢索bottle
的相關product
實體,但是當我嘗試像${bottle.product.name}
這樣檢索它時,我一直收到錯誤消息。 從相關product
實體獲取name
的正確方法是什么?
這是我得到的例外
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
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>
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();
}
}
WG服務
public class WGService {
public Long sendReorderRequestEmail(WGIO.ReorderBottles.ReorderRequest request) {
List<BottleReordering> bottles = request.getBottleReordering();
List<BottleReordering> requestedBottles = new ArrayList<>();
if(!CollectionUtils.isEmpty(bottles)) {
User user = userRepository.findUserById(request.getUserId());
bottles.forEach(rb -> {
BottleReordering bottle = new BottleReordering();
bottle.setProductId(rb.getProductId());
requestedBottles.add(bottle);
});
mailService.sendReorderEmail(user, requestedBottles);
}
}
郵件服務
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")
}
}
產品實體
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;
}
BottleReordering 實體
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;
}
在您的 Controller 中,您“僅”設置產品 ID
bottles.forEach(rb -> {
BottleReordering bottle = new BottleReordering();
bottle.setProductId(rb.getProductId());
requestedBottles.add(bottle);
});
您應該加載產品,例如
bottles.forEach(rb -> {
BottleReordering bottle = new BottleReordering();
bottle.setProductId(rb.getProductId());
bottle.setProcuct(productRepository.findById(rb.getProductId())) // <--- load product
requestedBottles.add(bottle);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.