25

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)
Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
Albinoswordfish
  • 1,929
  • 7
  • 33
  • 50

5 Answers5

16

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.
Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
  • 1
    I edited my code to do this: getEntityManager().refresh(getEntityManager().merge(product)); and I checked that I have the @Entity mapped. but still the same problem – Albinoswordfish Jan 16 '10 at 07:34
  • 2
    Does your getEntityManager() open a new EM every time? If so you are `merging` an entity in one hibernate session and `refreshing` it in another. – Luka Klepec Mar 27 '12 at 09:49
  • @Luka Klepec How do I check if my EntityManager open a new EM every time? I am Using Persistence Manager – Tushar Banne May 27 '16 at 12:54
15
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.

Daniel Szalay
  • 3,852
  • 12
  • 54
  • 100
8

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.

3

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" );
        }
    ...
Nick G.
  • 455
  • 9
  • 15
1

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!