EntityManager refresh problem

前端 未结 2 1391
礼貌的吻别
礼貌的吻别 2021-02-12 04:59

I\'m getting this error from my EntityManager when I call the refresh function.

public void saveProduct(Product product) {
    entityManager.refresh(product);
}
         


        
相关标签:
2条回答
  • 2021-02-12 05:36

    From the docs of EntityManager:

    IllegalArgumentException - if not an entity or entity is not managed

    1. Check if your entity is mapped (using @Entity, or with .xml configuration)
    2. Your entity must be persistent - i.e. managed by the entityManager. So if your entity is detached, merge() it first, and then refresh() it.
    0 讨论(0)
  • 2021-02-12 05:43
    public void saveProduct(Product product) {
        ...
    
        Product managedProductEntity = entityManager.find(Product.class, product.getId());
        entityManager.refresh(managedProductEntity);
    
        ...
    }
    

    Works this way. managedProductEntity will be managed, and therefore it can be refreshed from database.

    0 讨论(0)
提交回复
热议问题