Announcement Announcement Module
Collapse
No announcement yet.
Duplicate ContraintViolationException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Duplicate ContraintViolationException

    I have the following code using Hibernate 3:

    try {
    getHibernateTemplate().update(pojo);
    } catch (DataAccessException dae) {
    if (dae.getCause() instanceof StaleObjectStateException) {
    // record has been changed
    }
    else if (dae.getCause() instanceof ConstraintViolationException) {
    // possible duplication error.
    }
    else {
    // other outcome
    }
    }

    When updating I wish to catch entering of a duplicate record, so I can show a more friendly error message. However, checking the getCause() for ContraintViolationException does not occur despite updating a record to an existing record. I have read ideas of doing a prior select, which I am not to keen on.
    Any ideas/help would be appreciated. thanks

  • #2
    However, checking the getCause() for ContraintViolationException does not occur despite updating a record to an existing record
    Try flushing after the update.

    Comment


    • #3
      I have these methods:

      Code:
          public void saveResourceType(ResourceType resourceType) {
              getHibernateTemplate().saveOrUpdate(resourceType);
              getHibernateTemplate().flush();
              
          }
      
      public void saveResourceType(ResourceType resourceType) 
              throws ResourceTypeExistsException {
              try {
                  dao.saveResourceType(resourceType);
              } catch (DataIntegrityViolationException ex) {
                  throw new ResourceTypeExistsException("ResourceType '" + resourceType.getName() + "' already exists!");
              }
          }
      I call the saveResourceType and here is the error I get afterward in the log. I am expecting to see this ResourceTypeExistsException exception but it's completely different exception returns.
      Code:
      org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)
      	at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:49)
      	at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
      	at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
      	at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
      	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
      	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
      	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
      	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
      	at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:488)
      	at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:435)
      	at org.springframework.transaction.interceptor.TransactionAspectSupport.doCloseTransactionAfterThrowing(TransactionAspectSupport.java:294)
      	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:61)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
      	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
      	at $Proxy4.saveResourceType(Unknown Source)
      	at org.appsecure.webapp.action.ResourceTypeFormController.onSubmit(ResourceTypeFormController.java:55)
      	at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:248)
      	at org.appsecure.webapp.action.BaseFormController.processFormSubmission(BaseFormController.java:149)
      	at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:243)
      	at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
      	at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
      	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
      	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
      	at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
      	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:355)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
      	at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at org.appsecure.webapp.filter.GZIPFilter.doFilter(GZIPFilter.java:51)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
      	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at com.opensymphony.clickstream.ClickstreamFilter.doFilter(ClickstreamFilter.java:42)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at org.appsecure.webapp.filter.ActionFilter.doFilter(ActionFilter.java:119)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:172)
      	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:292)
      	at net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:84)
      	at net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter.doFilter(SecurityEnforcementFilter.java:182)
      	at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
      	at net.sf.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:153)
      	at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
      	at net.sf.acegisecurity.wrapper.ContextHolderAwareRequestFilter.doFilter(ContextHolderAwareRequestFilter.java:50)
      	at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
      	at net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:225)
      	at net.sf.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:303)
      	at net.sf.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:173)
      	at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:125)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
      	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
      	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
      	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
      	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
      	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
      	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
      	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
      	at java.lang.Thread.run(Thread.java:595)
      [appsecure] DEBUG [http-8080-Processor24] ResourceTypeController.handleRequest(28) | entering 'handleRequest' method...
      [/code]

      Comment

      Working...
      X