0

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!

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. I did that and still get the same error.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.