2016-04-02 2 views
0

J'ai 3 tables dans ma base de données - Réservation, Restaurant et RestaurantTable. En ce moment, j'essaie de créer une nouvelle réservation et l'une des étapes consiste à ajouter une table. Mais lorsque je tente d'ajouter cette table l'erreur suivante apparaît:org.hibernate.LazyInitializationException (Spring/Hibernate)

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

C'est ma classe Restaurant:

@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 

je pourrais changer « table » à FetchType.EAGER, mais qui provoque d'autres problèmes . Ma classe 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. 

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

Le fichier .jsp l'erreur est survenue dans:

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

Toute aide est appréciée!

Répondre

1

refactor ResturantTable et supprimer type d'extraction dans cette classe

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

add type d'extraction dans Resturant classe

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

et ajoutez cette ligne votre méthode bookingService de classe getBooking(id) pour initialiser toutes les données

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

booking votre méthode de service getBooking(id) return object

+0

Merci pour votre réponse. Je l'ai fait et toujours avoir la même erreur. – charliekelly

+0

@charliekelly ı modifier ma réponse –

+0

Merci, ça a marché! – charliekelly

1

Le chargement des entités apparentées en mode paresseux signifie que les entités se chargeront quand elles seront accédées pour la première fois à condition que la session soit ouverte et valide.

L'accès aux éléments lorsque la session est fermée renvoie un LazyInitializationException.

Assurez-vous que vous accédez aux éléments lorsque la session est ouverte ou que vous passez du mode paresseux au mode désireux (par défaut).