web-dev-qa-db-fra.com

Qu'est-ce qui cause une violation de contrainte unique lors de l'utilisation d'une SEQUENCE dans Hibernate?

J'utilise le code ci-dessous pour générer des ID uniques pour un champ id. Cela fonctionnait bien jusqu'à la semaine dernière. J'ai supprimé certaines entités à l'aide de l'interface Hibernate, puis j'ai commencé à donner une exception de violation de contrainte unique lorsque j'essaie d'insérer de nouveaux enregistrements.

Quelle pourrait être la cause de ce problème?

@SequenceGenerator(name = "ParamGenerator", sequenceName = "ParamSequence", allocationSize = 1)
public class Param extends IdNameEntity<Long> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ParamGenerator")
    private Long id;

Voici la méthode de suppression:

@Override
public void delete(final T t) {
    getCurrentSession().delete(t);
}

Voici le journal des erreurs:

2018-04-25 16:34:41 [http-nio-8081-exec-3] INFO  o.h.e.j.b.internal.AbstractBatchImpl - HHH000010: On release of batch it still contained JDBC statements
2018-04-25 16:34:41 [http-nio-8081-exec-3] ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [could not execute statement]
2018-04-25 16:34:41 [http-nio-8081-exec-3] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/admin_panel] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [XXXTTT_XXX.SYS_C00114023]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
Java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (XXXTTT_XXX.SYS_C00114023) violated

    at Oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.Java:447)
    at Oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.Java:396)
    at Oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.Java:951)
    at Oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.Java:513)
    at Oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.Java:227)
    at Oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.Java:531)
    at Oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.Java:208)
    at Oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.Java:1046)
    at Oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.Java:1336)
    at Oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.Java:3613)
    at Oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.Java:3694)
    at Oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.Java:1354)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.Java:204)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.Java:45)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.Java:2921)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.Java:3421)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.Java:89)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.Java:560)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.Java:434)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.Java:337)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.Java:39)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.Java:1295)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.Java:468)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.Java:3135)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.Java:2352)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.Java:485)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.Java:147)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.Java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.Java:231)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.Java:65)
    at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.Java:581)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.Java:761)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.Java:730)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.Java:504)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.Java:292)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.Java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.Java:656)
    at roma.admin_panel.orm.dao.ParamDao$$EnhancerBySpringCGLIB$$91c61766.save(<generated>)
    at roma.admin_panel.orm.service.AbstractService.save(AbstractService.Java:41)
    at roma.admin_panel.controller.ParamController.save(ParamController.Java:59)
    at Sun.reflect.GeneratedMethodAccessor121.invoke(Unknown Source)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.Java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:116)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.Java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.Java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.Java:729)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:230)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
    at org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:317)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.Java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.Java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.Java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.Java:137)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.Java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.Java:170)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.Java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.Java:200)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.Java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.Java:124)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.Java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.Java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.Java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.Java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.Java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.Java:177)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.Java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.Java:262)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.Java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.Java:105)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.Java:81)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.Java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107)
    at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:192)
    at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:165)
    at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:198)
    at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:96)
    at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:474)
    at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:140)
    at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:79)
    at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:87)
    at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:349)
    at org.Apache.coyote.http11.Http11Processor.service(Http11Processor.Java:783)
    at org.Apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.Java:66)
    at org.Apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.Java:798)
    at org.Apache.Tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.Java:1434)
    at org.Apache.Tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.Java:49)
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1142)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:617)
    at org.Apache.Tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.Java:61)
    at Java.lang.Thread.run(Thread.Java:748)
12
Salih Erikci

J'ai finalement trouvé le problème.

Lorsque nous avons créé cette base de données pour la première fois, nous avons migré les données de la base de données de test.
.

7
Salih Erikci

Méthode 1: Modifier la valeur de début de séquence

En supposant que vous utilisez SQL Developer:

  1. Développez le nœud "Séquences" sous votre base de données dans la fenêtre "Connexions".
  2. Recherchez et cliquez avec le bouton droit sur la séquence "ParamSequence".
  3. Sélectionnez "Modifier ..."
  4. Définissez la valeur "Commencer par" de manière appropriée. Cela peut être découvert au préalable en exécutant une requête telle que SELECT MAX(ID) FROM PARAM; puis en y ajoutant la valeur "Incrément" (par exemple 1). Les détails exacts ici dépendront du nom de la table/du champ.

...ou...

Méthode 2: Modifier la valeur NEXTVAL de la séquence

Si vous préférez modifier uniquement la valeur NEXTVAL, il existe une solution de contournement sournoise dans cette réponse . Copié ci-dessous pour votre contexte:

ALTER SEQUENCE ParamSequence INCREMENT BY 100;
SELECT ParamSequence.NEXTVAL FROM DUAL;
ALTER SEQUENCE ParamSequence INCREMENT BY 1;
0
Steve Chambers

Cela vous donnera probablement quelques indices:

Les séquences Oracle sont garanties de ne pas générer de valeurs en double. Si vous voyez des valeurs en double, l'une des deux choses doit se produire. Vous devez avoir rencontré un bogue Oracle assez obscur ou il doit y avoir une erreur dans votre application (c'est-à-dire que quelque chose met en cache une valeur de séquence de manière incorrecte, la stockant de manière non sécurisée pour les threads, etc.)

source: https://community.Oracle.com/thread/860691

0
Reddy_73