web-dev-qa-db-ja.com

HibernateでSEQUENCEを使用すると、一意制約違反の原因は何ですか?

以下のコードを使用して、idフィールドの一意のIDを生成しています。先週までは順調でした。 Hibernateインターフェースを使用して一部のエンティティを削除した後、新しいレコードを挿入しようとすると、一意制約違反の例外が発生し始めました。

この問題の原因は何ですか?

@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;

これは削除メソッドです:

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

これはエラーログです:

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

ようやく問題が見つかりました。

このDBを最初に作成したときに、テストDBからデータを移行しました。
テストDBから転送されたデータはprod dbのシーケンスを使用していないため、シーケンスによって生成されたIDが事前に挿入されたレコードと競合するため、レコードをprod dbに挿入しようとすると、制約違反例外が発生しました。

7
Salih Erikci

方法1:シーケンスの開始値を変更する

SQL Developerを使用していると仮定します。

  1. [接続]ウィンドウで、データベースの下の[シーケンス]ノードを展開します。
  2. 「ParamSequence」シーケンスを見つけて右クリックします。
  3. 「編集...」を選択します
  4. 「開始」の値を適切に設定します。 これは、SELECT MAX(ID) FROM PARAM;などのクエリを実行してから、 "Increment"値(1など)を追加することで事前に確認できます。ここでの正確な詳細は、テーブル/フィールド名によって異なります。

...または...

方法2:シーケンスのNEXTVAL値を変更する

NEXTVALの値のみを変更したい場合は、 この答え に卑劣な回避策があります。あなたのコンテキストのために以下をコピーしました:

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

おそらくこれはあなたにいくつかのヒントを与えるでしょう:

Oracleシーケンスは、重複した値を生成しないことが保証されています。値が重複している場合は、2つの原因のいずれかが発生しているはずです。かなりあいまいなOracleのバグにぶつかったか、アプリケーションにエラーがあるはずです(つまり、シーケンス値が誤ってキャッシュされている、スレッドセーフではない方法で保存されているなど)。

ソース: https://community.Oracle.com/thread/860691

0
Reddy_73