HỎI mình cần hỏi về cách lấy value từ entity quan hệ one to many để get lên jsp ạ.

lamsun99

New Member
4/6/20
1
0
1
Quảng Trị
chào mọi người mình đang làm chức năng hiển thị danh sách nhưng bị lỗi ở chỗ ko get dc tên images lên mong mọi người giúp đỡ
View attachment 2438

product entity
Java:
@Entity
@Table(name = "product")
public class ProductEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(length = 100, nullable = false)
    private String name;

    @Column(length = 1000)
    private String description;

    @Column(nullable = false)
    private double price;

    @Column(name = "publish_date")
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date publishDate;

    @Column(nullable = false, columnDefinition = "TINYINT", length = 1)
    private boolean active;

    @ManyToOne
    @JoinColumn(name = "category_id")
    private CategoryEntity category;

    @OneToMany(mappedBy = "product", fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    private List<ImageEntity> images;

    public ProductEntity() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public CategoryEntity getCategory() {
        return category;
    }

    public void setCategory(CategoryEntity category) {
        this.category = category;
    }

    public List<ImageEntity> getImages() {
        return images;
    }

    public void setImages(List<ImageEntity> images) {
        this.images = images;
    }
    
}
image entity
Java:
@Entity
@Table(name = "image")
public class ImageEntity implements Serializable{
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    private String name;
    
    @ManyToOne
    @JoinColumn(name = "product_id")
    private ProductEntity product;
    
    public ImageEntity() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ProductEntity getProduct() {
        return product;
    }

    public void setProduct(ProductEntity product) {
        this.product = product;
    }
    
}
homecontroller
Java:
@Controller
public class HomeController {
    
    @Autowired
    private ProductService productService;
    
    @RequestMapping(value = {"/", "/home"}, method = RequestMethod.GET)
    public String home(Model model){
        model.addAttribute("products", productService.getProducts());
        return "home";
    }
}
HTML:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>List product</title>
    </head>
    <body>
        <h1>List Product</h1>
        <table>
            <tr>
                <th>Stt</th>
                <th>name</th>
                <th>description</th>
                <th>price</th>
                <th>publish Date</th>
                <th>active</th>
                <th>category</th>
                <th>images</th>
            </tr>
            <c:forEach items="${products}" var="item">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.description}</td>
                    <td>${item.price}</td>
                    <td>${item.publishDate}</td>
                    <td>${item.active}</td>
                    <td>${item.category.name}</td>
                    <td>${item.images.name}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>