Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityManager refresh problem

Tags:

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) 
like image 774
Albinoswordfish Avatar asked Jan 15 '10 07:01

Albinoswordfish


People also ask

How do I refresh EntityManager?

You need to refresh() the object, or mark it as invalid to be refreshed the next time it is queried. But if you try to refresh a detached entity, you will get a "java. lang. IllegalArgumentException: Entity not managed".

What is EntityManager flush?

The EntityManager. flush() operation can be used the write all changes to the database before the transaction is committed. By default JPA does not normally write changes to the database until the transaction is committed. This is normally desirable as it avoids database access, resources and locks until required.

What happens if EntityManager is not closed?

If you don't close it your entities will be kept as attached, even after you're done using them. Your context will be kept alive even when you can no longer access your EM.

Does the EntityManager use hibernate?

Hibernate provides implementation of JPA interfaces EntityManagerFactory and EntityManager .


2 Answers

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.
like image 95
Bozho Avatar answered Sep 18 '22 12:09

Bozho


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.

like image 26
Daniel Szalay Avatar answered Sep 18 '22 12:09

Daniel Szalay