简体   繁体   中英

org.hibernate.LazyInitializationException (Spring/Hibernate)

I have 3 tables in my database - Booking, Restaurant and RestaurantTable. Right now I am trying to create a new booking and one of the steps there is adding a table. But when I try to add this table the following error comes up:

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

This is my Restaurant class:

@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

I could change "table" to FetchType.EAGER, but that causes other problems. My RestaurantTable class:

@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.

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

The .jsp file the error occured in:

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

Any help is appreciated!

refactor ResturantTable and delete fetch type in this class

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

add fetch type in Resturant class

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

and add this line your bookingService class method getBooking(id) for initalize all data

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

booking your service method getBooking(id) return object

Loading the related entities in lazy mode means that entities will load when they are accessed for the first time provided that the session should be open and valid.

Accessing the items when session is closed throws a LazyInitializationException .

Make sure you access the items when session is open or change the fetch type mode from lazy to eager(default).

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