我新來休眠,我不是爲什麼我有這個錯誤。我評論了我的DAO代碼進行更新,並且hibernate仍在執行更新查詢。這是我的服務代碼。爲什麼hibernate自己執行更新?
@Override
@Transactional(readOnly = false)
public void updateProduct(Product productToUpdate) throws DuplicateProductException {
Product product = productDao.findByProductId(productToUpdate.getProductId());
if (productDao.findByName(productToUpdate.getName()) != null
&& !product.getName().equals(productToUpdate.getName())) {
throw new DuplicateProductException();
}
product.setName(productToUpdate.getName());
product.setCategory(productToUpdate.getCategory());
product.setPrice(productToUpdate.getPrice());
product.setImage(productToUpdate.getImage());
// productDao.updateProduct(product);
}
我評論DAO和休眠仍在執行查詢。
這是我的控制器的代碼。
@RequestMapping(value = "/updateProduct", method = RequestMethod.POST)
public String updateProductPost(@Validated @ModelAttribute("product") ProductHelper productHelper,
BindingResult bindingResult, Model model) throws CategoryNotFoundException {
model.addAttribute("categories", categoryService.findAll());
model.addAttribute("activePage", AdminPage.UPDATE_PRODUCT);
updateProductValidator.validate(productHelper, bindingResult);
if (bindingResult.hasErrors()) {
return "admin_home";
}
Product product = productHelper.buildProductToUpdate(productService, categoryService);
try {
productService.updateProduct(product);
model.addAttribute("updatedProduct", product);
} catch (DuplicateProductException e) {
model.addAttribute("duplicateProduct", product);
}
return "admin_home";
}
有什麼奇怪的是,我有我的整個DAO代碼註釋:
// @Override
// public void updateProduct(Product product) {
//// sessionFactory.getCurrentSession().update(product);
// }
而且還在冬眠正在執行一個更新查詢:
Hibernate: update PRODUCT set category_id=?, image=?, name=?, price=?, product_id=? where id=?
Hibernate: select product0_.id as id1_3_, product0_.category_id as category6_3_, product0_.image as image2_3_, product0_.name as name3_3_, product0_.price as price4_3_, product0_.product_id as product_5_3_ from PRODUCT product0_ where product0_.product_id=?
Hibernate: select product0_.id as id1_3_, product0_.category_id as category6_3_, product0_.image as image2_3_, product0_.name as name3_3_, product0_.price as price4_3_, product0_.product_id as product_5_3_ from PRODUCT product0_ where product0_.name=?
如果這是一些初學者錯誤,我真的很抱歉,但我對hibernate相當陌生。謝謝。
當實體附加並且您更改其某個屬性,Hibernate會自動將您對數據庫中相應行進行的更改(在刷新/提交時)保存。 http://stackoverflow.com/questions/5260297/how-does-the-detached-object-work-in-hibernate – user3871754
我該如何解決這個問題?這是我第一次遇到這個問題。我以前從未遇到更新問題。 – saluyotamazing
[事務性保存而不調用更新方法]可能重複(http://stackoverflow.com/questions/8190926/transactional-saves-without-calling-update-method) – daredesm