Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问一对多关系_Java_Spring_Spring Boot_Thymeleaf - Fatal编程技术网

Java 访问一对多关系

Java 访问一对多关系,java,spring,spring-boot,thymeleaf,Java,Spring,Spring Boot,Thymeleaf,我正在使用Spring Boot和ThymeLeaf发送电子邮件。在电子邮件模板中,我需要为瓶子检索相关的产品实体,但是当我尝试像这样检索${battle.product.name}时,我总是会收到一个错误。从相关的产品实体获取名称的正确方法是什么 这是我得到的一个例外 Exception evaluating SpringEL expression: "bottle.product.name" (template: "reorder_request.html&qu

我正在使用
Spring Boot
ThymeLeaf
发送电子邮件。在电子邮件模板中,我需要为
瓶子
检索相关的
产品
实体,但是当我尝试像这样检索
${battle.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}">&nbsp;</span></td>  <!-- trying to get the related product entity -->
    </tr>
</tbody>

在控制器中,“仅”设置产品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);
 });
瓶子。forEach(rb->{
瓶装再订购瓶=新瓶装再订购();
setProductId(rb.getProductId());
battle.setprocc(productRepository.findById(rb.getProductId())//
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;

}
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;

}
 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);
 });