简体   繁体   中英

JPA Hibernate will not read (lazy loading) more then 1 member from an entity

Hi I have the following problem.

I need to make a collection of unshipped orders to read them in a JSP file.

When I doPost (post the form with id's to ship...) I get an exception when I have more than 1 orders in the collection which are not shipped: javax.el.ELException: Error reading 'name' on type entities.Customer_$$_jvstf5f_1 root cause: org.hibernate.LazyInitializationException: could not initialize proxy - no Session

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (request.getParameterValues("ship") != null) {
        List<Order> noStockUnshippedOrders = new ArrayList<>();

        for (String orderId : request.getParameterValues("ship")) {
            try {
                Long id = Long.parseLong(orderId);
                if (!orderService.setAsShipped(id)) {
                    noStockUnshippedOrders.add(orderService.read(id));
                }
            } catch (NumberFormatException e) {
                // IF NO LONG DO NOTHING
            }
        }
        request.setAttribute("noStockUnshippedOrders", noStockUnshippedOrders);
    }
    getUnshippedOrders(request, response);
    // GET ON WITH IT
    request.getRequestDispatcher(VIEW).forward(request, response);
}

and this is my JSP code:

<!doctype html>
<c:forEach items='${noStockUnshippedOrders}' var='noStockUnshippedOrder'>
            <c:url value='/orderdetail.htm' var='orderDetailURL'>
                <c:param name='id' value="${noStockUnshippedOrder.id}" />
            </c:url>
            <tr>
                <td class="col-md-1 text-center"><a class="btn btn-danger"
                    href="<c:out value='${orderDetailURL}'/>" 
 role="button">${noStockUnshippedOrder.id}</a></td>
                <td class="col-md-1">${noStockUnshippedOrder.orderDate}</td>
                <td class="col-md-1">${noStockUnshippedOrder.requiredDate} 
 </td>
                <td class="col-md-2">${noStockUnshippedOrder.customer.name}
</td>
                <td>${noStockUnshippedOrder.comments}</td>
                <td class="col-md-2"><img
                    src="images/${noStockUnshippedOrder.status}.png">&nbsp;

${fn:toUpperCase(fn:substring(noStockUnshippedOrder.status, 0, 1))}${fn:toLowerCase(fn:substring(noStockUnshippedOrder.status, 1, -1))}
                </td>
            </tr>
        </c:forEach>

Probability the error occurs here ${noStockUnshippedOrder.comments} or another attribute of order(if attribute is fetch = FetchType.LAZY), because Hibernate session is closed, in this moment.

*The first solution is to use strategy OpenSessionInViewFilter strategy. See this link: https://developer.jboss.org/wiki/OpenSessionInView

*The second solution is find the list of comments(or another attribute lazy) in Servlet, example.

if (!orderService.setAsShipped(id)) {
    Order order = orderService.read(id);
    List<Comment> comments = commentService.findByOrder(order);
    order.setComments(comments);
    noStockUnshippedOrders.add(order);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM