繁体   English   中英

org.hibernate.LazyInitializationException(Spring / Hibernate)

[英]org.hibernate.LazyInitializationException (Spring/Hibernate)

我的数据库中有3个表-Booking,Restaurant和RestaurantTable。 现在,我正在尝试创建一个新的预订,其中的一个步骤是添加一张桌子。 但是,当我尝试添加此表时,出现以下错误:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

这是我的餐厅课:

@Entity
@Table(name="restaurant")
public class Restaurant {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    @Column(name="restaurant_name")
    private String restaurantName;

    @Column(name="address")
    private String address;

    @OneToMany(mappedBy = "restaurant")
    private Set<RestaurantTable> table;

    // Getters and setters

我可以将“表”更改为FetchType.EAGER,但这会导致其他问题。 我的RestaurantTable类:

@Entity
@Table(name="restaurant_table")
public class RestaurantTable {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    @Column(name="table_size")
    private Integer tableSize;

    @Column(name="table_number")
    private Integer tableNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="restaurant_id")
    private Restaurant restaurant;

    // Getters and setters.

我的BookingController.java:

@RequestMapping(value = "booking/create/{id}", method = RequestMethod.GET)
public String chooseTable(@PathVariable Long id, Model model) {
    Booking booking = bookingService.getBooking(id);
    Restaurant restaurant = booking.getRestaurant();
    Set<RestaurantTable> tableSet = restaurant.getTable();
    model.addAttribute("tables", tableSet);
    model.addAttribute("booking", booking);
    return "chooseTable";
}

.jsp文件,错误发生在:

<body>
<jsp:include page="../fragments/menu.jsp"/>
<div id="body">
    <h2>Create new booking</h2>

    <form:form method="POST" modelAttribute="booking" >
        <table>
            <tr>
                <td>Choose a table*:</td>
                <td><form:select path="tableNumber">
                        <form:option value="" label="--- Select ---" />
                        <form:options items="${tables}" itemValue="tableNumber" itemLabel="tableNumber"/>
                </form:select>
            </tr>
            <tr>
                <td colspan="3"><input type="submit" /></td>
            </tr>
        </table>
    </form:form>
    <div>
        <a href="/bookings">Back to List</a>
    </div>
</div>
<jsp:include page="../fragments/footer.jsp"/>

</body>

任何帮助表示赞赏!

重构ResturantTable并删除此类中的获取类型

    @ManyToOne        
    @JoinColumn(name="restaurant_id")
    private Restaurant restaurant;

Resturant类中添加访Resturant类型

@OneToMany(mappedBy = "restaurant",fetch = FetchType.LAZY)
private Set<RestaurantTable> table;

并将这行添加到bookingService类方法getBooking(id)getBooking(id)所有数据

booking.getRestaurant().getTable().size();

booking您的服务方法getBooking(id)返回对象

在延迟模式下加载相关实体意味着,如果会话应该是开放且有效的,则当实体首次被访问时它们将被加载。

在会话关闭时访问项目将引发LazyInitializationException

确保在会话打开时访问项目,或将获取类型模式从“懒惰”更改为“渴望”(默认)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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