通过对象删除作为参数不起作用

时间:2013-11-28 12:11:24

标签: spring hibernate spring-mvc jpa

朋友您好我正在关注Spring + Hibernate + JPA。 我正在尝试在我的数据库上执行删除操作。

当我从控制器传递模型的对象时,如下所示做不起作用

@Transactional(readOnly = false)
@Override
public void doDeleteCategory(Category deleteCategory) {
    try {
        entityManager.remove(deleteCategory);
    } catch (Exception exc) {
        logger.info(exc.getStackTrace().toString());
    }
}

但是,当我再次检索相同的对象时,它的工作方式如下

@Transactional(readOnly = false)
@Override
public void doDeleteCategory(Category deleteCategory) {
    Category category = retrieveCategory(deleteCategory.getCategoryId());
    try {
        entityManager.remove(deleteCategory);
    } catch (Exception exc) {
        logger.info(exc.getStackTrace().toString());
    }
}

我很想知道为什么会发生这种情况,而创建和更新/合并工作完美时我会通过这两种方法做到这一点。

这是堆栈跟踪:

org.apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'oltapp'
Nov 28, 2013 1:46:36 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Nov 28, 2013 1:47:14 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Nov 28, 2013 1:47:14 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Nov 28, 2013 1:47:25 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'oltapp'
Nov 28, 2013 1:53:56 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [oltapp] in context with path [/oltapp] threw exception [Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly] with root cause
javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:72)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:392)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy263.doDeleteCategory(Unknown Source)
at co.softwarehouse.olt.service.category.CategoryServiceImpl.doDeleteCategory(CategoryServiceImpl.java:68)
at co.softwarehouse.olt.web.controller.CategoryController.deleteCategory(CategoryController.java:98)
at co.softwarehouse.olt.web.controller.CategoryController.processCategory(CategoryController.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:0)

merge的重点是获取一个分离的实体并将其状态合并到一个附加的实体。显然,合并与分离实体的工作。

remove的重点是获取附加实体并将其删除,因此您需要一个附加实体来删除它。 The javadoc明确了这一主题:

  

void remove(java.lang.Object entity)

     

删除实体实例。

     

抛出:

     
      
  • IllegalArgumentException - 如果实例不是实体或是分离的实体

  •   
  • TransactionRequiredException - 如果在PersistenceContextType.TRANSACTION类型的容器管理实体管理器上调用且没有事务

  •   

(强调我的)