【发布时间】:2016-07-17 15:18:19
【问题描述】:
现在我的数据库中有 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);
}
【问题讨论】:
-
请显示 updateRestaurant 方法
-
@itpr 我编辑了我的帖子。感谢您的帮助!
标签: java mysql spring hibernate jsp