2016-03-29 16 views
1

今、私のデータベースにはBooking、Restaurant、RestaurantTableの3つのテーブルがあります。私はRestaurantとRestaurantTableの間に1対多のマッピングを持っています(レストランには多くのテーブルがありますが、テーブルには1つしかレストランがありません)。新しいテーブルをレストランに挿入する "newTable.jsp"というファイルがあります。私はそれが私に次のエラーを与えることをやるしようとすると、しかし:バッチ更新が予期しない行数を返す(Spring/Hibernate)

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 

私はまだ存在していないRestaurantTableにアクセスしようとしていると思いますか?しかし、私はそれを修正する方法を知らない。ここで

は私の "Restaurant.java" クラスです:

@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 

マイ "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 

マイ "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> 

マイRestaurantTableController :

@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の{id}はレストランIDで、 "editRestaurant.jsp"から渡されます。どんな助けもありがとうございます。

EDIT:私のupdateRestaurant方法:

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

updatemerge方法を使用してみてくださいupdateRestaurant方法を表示する、してください私は私のポストを編集し@itpr – itpr

+0

。助けてくれてありがとう! – charliekelly

答えて

1

RestaurantTableエンティティがその時にIDを持っているので、スロー例外を休止しません。代わりに

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.

関連する問題