2016-03-29 14 views
1

Veritabanımda şu anda 3 tane tablom var - Rezervasyon, Restoran ve RestaurantTable. Restoran ve RestaurantTable (bir restoran birçok tabloya sahip olabilir, ancak bir tablo sadece bir restoran olabilir) arasında bir çok haritalama var. Restorana yeni tablolar ekleyen "newTable.jsp" adlı bir dosyam var. Ama bana aşağıdaki hatayı veriyor bunu çalıştığınızda:Toplu güncelleştirme, güncellemeden beklenmeyen satır sayısı geri geldi (İlkbahar/Hazırda Beklet)

Request processing failed; nested exception is org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 

ben henüz var olmayan bir RestaurantTable erişmeye çalışıyor? Ama bunu nasıl düzelteceğimi bilmiyorum.

@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", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    private Set<RestaurantTable> table; 

    // Getters and setters 

Benim "RestaurantTable.java":

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

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

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

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

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

    // Getters and setters 

Benim "newTable.jsp"

<body> 
<jsp:include page="../fragments/menu.jsp"/> 
<div id="body"> 
    <section class="content-wrapper main-content clear-fix"> 

     <h2>Add New Table</h2> 

     <form:form method="POST" commandName="table" modelAttribute="table"> 
      <table> 
       <tr> 
        <td>Table size:</td> 
        <td><form:input path="tableSize" /></td> 
       </tr> 
       <tr> 
        <td>Table number:</td> 
        <td><form:input path="tableNumber" /></td> 
       </tr> 
       <tr> 
        <td colspan="3"><input type="submit" /> 
        </td> 
       </tr> 
      </table> 
     </form:form> 

    </section> 
</div> 
<jsp:include page="../fragments/footer.jsp"/> 

</body> 

Benim RestaurantTableController İşte

benim "Restaurant.java" sınıftır :

@Controller 
public class RestaurantTableController { 


    @Autowired 
    private RestaurantService restaurantService; 

    @Autowired 
    private RestaurantTableService restaurantTableService; 

    @RequestMapping(value="restaurant/table/{id}", method = RequestMethod.GET) 
    public String addRestaurantTable(Model model) { 
     model.addAttribute("table", new RestaurantTable()); 
     return "newTable"; 
    } 

    @RequestMapping(value = "restaurant/table/{id}", method = RequestMethod.POST) 
    public String addRestaurantTable(@PathVariable Long id, @ModelAttribute ("table") RestaurantTable table) { 
     // Get a Restaurant object and add the table to it. 
     Restaurant restaurant = restaurantService.getRestaurant(id); 
     Set<RestaurantTable> tableSet = restaurant.getTable(); 
     tableSet.add(table); 
     restaurant.setTable(tableSet); 
     restaurantService.updateRestaurant(restaurant); 
     return "editRestaurant"; 
    } 

} 

RestaurantTableController'daki {id}, bir Restaurant kimliği, "editRestaurant.jsp" adresinden iletilir. Herhangi bir yardım takdir edilir.

DÜZENLEME: My updateRestaurant yöntemi:

@Override 
public void updateRestaurant(Restaurant restaurant) { 
    Session session = this.sessionFactory.getCurrentSession(); 
    session.update(restaurant); 
    logger.info("Restaurant record updated successfully, Restaurant Details=" + restaurant); 
} 
+0

update ait merge yöntemini kullanmayı deneyin updateRestaurant yöntemini göster, lütfen Gönderimimi düzenlenmiş @itpr – itpr

+0

. Yardım için teşekkürler! – charliekelly

cevap

1

RestaurantTable varlık o zaman kimliğine sahip, bu yüzden atış istisna kış uykusuna etmez. yerine

update

Update the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown.