EntityManager刷新问题

Alb*_*ish 21 orm spring hibernate jpa entitymanager

当我调用刷新函数时,我从我的EntityManager收到此错误.

public void saveProduct(Product product) {
    entityManager.refresh(product);
}
Run Code Online (Sandbox Code Playgroud)

我听说这可能是Spring/Hibernate的一个错误,但是我不知道如何解决这个问题.

编辑:错误是

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)
Run Code Online (Sandbox Code Playgroud)

Boz*_*zho 15

来自以下文件EntityManager:

IllegalArgumentException - 如果不是,则不管理实体或实体

  1. 检查您的实体是否已映射(使用@Entity.xml配置)
  2. 您的实体必须是持久的 - 即由entityManager管理.因此,如果你的实体是分离的,merge()那么首先是它,然后是refresh()它.

  • 你的getEntityManager()每次都打开一个新的EM吗?如果是这样,你在一个休眠会话中"合并"一个实体,在另一个休眠会话中"刷新"它. (2认同)

Dan*_*lay 13

public void saveProduct(Product product) {
    ...

    Product managedProductEntity = entityManager.find(Product.class, product.getId());
    entityManager.refresh(managedProductEntity);

    ...
}
Run Code Online (Sandbox Code Playgroud)

这样工作.managedProductEntity将被管理,因此可以从数据库刷新.


小智 8

如果product刚刚创建了对象,则不能refresh(),因为数据库中没有包含对象原始值的行.你首先要persist()product,然后flush()EntityManager的,后一个refresh()是可能的.