IllegalArgumentException: 未知实体: java.lang.Integer]

我在学习 spring 时做了这个例子,一切正常,现在我正在编写自己的项目,我不明白为什么当我尝试从EntityManager 类从 MySQL 数据库中删除对象


产品类(我不会在此处粘贴所有方法,例如 getter 和 setter 等)


import javax.persistence.*;


@Entity

public 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(我只显示删除方法)


@Service

public class ProductService {

    public void deleteProduct(Integer id) {

        productRepository.deleteProduct(id);

    }

    //more code...

}

产品控制器


@Controller

public 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...

}


呼如林
浏览 109回答 1
1回答

富国沪深

EntityManager#remove将实体实例作为参数。你正在路过Integer。改成:@Override@Transactionalpublic void deleteProduct(Product product) {    entityManager.remove(product);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java