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!