EntityManager refresh problem

二次信任 提交于 2019-12-05 09:39:31

问题


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

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

I heard this could be a bug with Spring/Hibernate, however I'm not sure how to get around this.

Edit: the error is

java.lang.IllegalArgumentException: Entity not managed
org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:268)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)
$Proxy17.refresh(Unknown Source)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:198)
$Proxy11.refresh(Unknown Source)
springapp.repository.JdbcProductDao.saveProduct(JdbcProductDao.java:66)
springapp.service.SimpleProductManager.increasePrice(SimpleProductManager.java:28)
springapp.web.PriceIncreaseFormController.onSubmit(PriceIncreaseFormController.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

回答1:


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.



回答2:


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.




回答3:


If the product object has just been created, you can't refresh() it, because there is no row in the database with the original values of the object. You first have to persist() the product and then flush() the entitymanager, after that a refresh() is possible.




回答4:


If an object is detached, it can't be refreshed either. Wonder if it might be a bug... Just take a look at lines 730-733 of AbstractEntityManagerImpl (Hibernate 3.6.0.Final ?):

    public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
    ...
        if ( !getSession().contains( entity ) ) {
            throw new IllegalArgumentException( "Entity not managed" );
        }
    ...



回答5:


Passing a null entity will return this same error. We had this problem in our app when we first implemented refresh routines and couldn't make sense of it since the entities were all managed. A null instance of a managed entity obviously doesn't count!



来源:https://stackoverflow.com/questions/2070073/entitymanager-refresh-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!