批量更新从更新(Spring / Hibernate)

时间:2016-03-29 20:57:46

标签: java mysql spring hibernate jsp

现在我的数据库中有3个表 - Booking,Restaurant和RestaurantTable。我在Restaurant和RestaurantTable之间有一对多的映射(餐厅可以有很多桌子,但桌子上只有一个餐厅)。我有一个名为“newTable.jsp”的文件,它将新表格插入餐厅。但是当我尝试这样做时,它会给我以下错误:

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”传递的。任何帮助表示赞赏。

编辑:我的updateRestaurant方法:

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

1 个答案:

答案 0 :(得分:2)

RestaurantTable实体当时没有ID,所以hibernate抛出异常。尝试使用merge方法而不是update

  

update

     

使用给定分离实例的标识符更新持久化实例。如果存在具有相同标识符的持久化实例,则抛出异常。