Hibernate OneToMany列表重新加载

时间:2018-06-01 10:27:30

标签: hibernate jsp servlets

我使用JSP / Servlet和Hibernate来创建web。

我有2个Product和ProductItem类,我可以通过使用JSTL核心标记$ {fn:length(pr.productItemList)}来计算每个产品的ProductItem数量。 问题是当我删除或插入ProductItem时,List没有重新加载,所以计数结果没有改变,但是当我清理并构建项目时,结果发生了变化。

所以任何重新加载列表的想法,请帮助。

这是我的产品类

public class Product implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "ProductID")
    private Integer productID;
    @Size(max = 100)
    @Column(name = "ProductName")
    private String productName;
    @Size(max = 1000)
    @Column(name = "ProductInfo")
    private String productInfo;
    @Column(name = "Price")
    private Float price;
    @JoinColumn(name = "BrandID", referencedColumnName = "BrandID")
    @ManyToOne
    private Brand brandID;
    @JoinColumn(name = "CategoryID", referencedColumnName = "CategoryID")
    @ManyToOne
    private Category categoryID;
    @OneToMany(mappedBy = "productID" , fetch = FetchType.EAGER)
    private List<ProductItem> productItemList;

这是我的ProductItem类

public class ProductItem implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "ProductItemID")
    private String productItemID;
    @Column(name = "Deleted")
    private Boolean deleted;
    @JoinColumn(name = "ProductID", referencedColumnName = "ProductID")
    @ManyToOne(fetch=FetchType.EAGER)
    private Product productID;

这是我的product.jsp页面

<table border="1" cellspacing="0" cellpadding="5">
            <caption><h1>List of Product</h1></caption>
            <thead>
                <tr>
                    <th>Product ID</th>
                    <th>Product Amount</th>
                    <th>Product Name</th>
                    <th>Product price</th>
                    <th>Category Name</th>                    
                    <th>Brand Name</th>    

                </tr>
            </thead>
    <c:forEach items="${list}" var="pr">
                <tr>
                    <td>${pr.productID}</td>
                    <td>${fn:length(pr.productItemList)}</td>                    
                    <td>${pr.productName}</td>                       
                    <td>${pr.price}</td>                        
                    <td>${pr.categoryID.categoryName}</td>                        
                    <td>${pr.brandID.brandName}</td> 
                   
                 </tr>
    </c:forEach>

</tbody>
</table> 

和我的ProductListServlet

public class ProductListServlet extends HttpServlet {


    @EJB
    private ProductFacadeLocal productFacade;
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setAttribute("list", productFacade.findAll()); 
        request.getRequestDispatcher("Product.jsp").forward(request, response);

 }

0 个答案:

没有答案