我在学习 spring 时做了这个例子,一切正常,现在我正在编写自己的项目,我不明白为什么当我尝试从EntityManager 类从 MySQL 数据库中删除对象产品类(我不会在此处粘贴所有方法,例如 getter 和 setter 等)import javax.persistence.*;@Entitypublic class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String name; @OneToOne private ProductType type; private float price; private String description; //more code...}ProductRepository(我只显示删除方法)import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.transaction.Transactional;public class DBProductRepository implements ProductRepository{ @PersistenceContext private EntityManager entityManager; @Override @Transactional public void deleteProduct(Integer id) { entityManager.remove(id); } //more code...}ProductService(我只显示删除方法)@Servicepublic class ProductService { public void deleteProduct(Integer id) { productRepository.deleteProduct(id); } //more code...}产品控制器@Controllerpublic class ProductController { @RequestMapping("/products") public String getProducts(Model model){ List<Product> products = productService.getAllProducts(); model.addAttribute("products",products); return "products"; } @RequestMapping(value="/product/delete/{id}") public String deleteProduct(@PathVariable("id") Integer id){ productService.deleteProduct(id); return "redirect:/products"; } //more code...}
1 回答
富国沪深
TA贡献1790条经验 获得超9个赞
EntityManager#remove
将实体实例作为参数。你正在路过Integer
。
改成:
@Override
@Transactional
public void deleteProduct(Product product) {
entityManager.remove(product);
}
添加回答
举报
0/150
提交
取消