oracle数据导入时,提示违反唯一约束性?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle数据导入时,提示违反唯一约束性?相关的知识,希望对你有一定的参考价值。

导入dmp文档时出现违反唯一约束性错误的原因,一般是该文档已经导入过一次,数据库中已经有与dmp文档相同内容,并且这些数据当中肯定有唯一约束性限制。
如果想多次导入dmp文档,最好在命令提示符(DOS)下进行,并选项“对象已存在,忽略创建错误”选择no就可以了。如果选择yes,则导入的数据记录会重复的,还可能出现违反唯一约束性错误。
参考技术A 详细阅读导入时Oracle报的错误信息,就可以知道是哪个表的哪个约束造成的这个问题。

根节点捆绑的唯一约束违反,Apache Jackrabbit 与 Oracle

【中文标题】根节点捆绑的唯一约束违反,Apache Jackrabbit 与 Oracle【英文标题】:Unique Constraint Violation on Root Node Bundle, Apache Jackrabbit with Oracle 【发布时间】:2017-01-22 17:42:32 【问题描述】:

目前处于评估将 Apache Jackrabbit 与我们的 Oracle DB 结合使用的早期阶段。在测试 repository.xml 配置的初始设置时,出现此错误:

15:18:32.281 [main] ERROR o.a.j.c.p.p.BundleDbPersistenceManager - FATAL error while writing the bundle: deadbeef-cafe-babe-cafe-babecafebabe
java.sql.SQLException: ORA-00001: unique constraint (REPO_BUNDLE_IDX) violated

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:754) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:972) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1192) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3415) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3521) ~[ojdbc14-10.2.0.4.jar:Oracle JDBC Driver version - "10.2.0.5.0"]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172) ~[commons-dbcp-1.3.jar:1.3]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172) ~[commons-dbcp-1.3.jar:1.3]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172) ~[commons-dbcp-1.3.jar:1.3]
    at org.apache.jackrabbit.core.util.db.ConnectionHelper.execute(ConnectionHelper.java:515) ~[jackrabbit-data-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.util.db.ConnectionHelper.reallyUpdate(ConnectionHelper.java:344) ~[jackrabbit-data-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.util.db.ConnectionHelper$2.call(ConnectionHelper.java:331) ~[jackrabbit-data-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.util.db.ConnectionHelper$2.call(ConnectionHelper.java:327) ~[jackrabbit-data-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.util.db.ConnectionHelper$RetryManager.doTry(ConnectionHelper.java:536) ~[jackrabbit-data-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.util.db.ConnectionHelper.update(ConnectionHelper.java:327) ~[jackrabbit-data-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.persistence.pool.BundleDbPersistenceManager.storeBundle(BundleDbPersistenceManager.java:950) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.persistence.bundle.AbstractBundlePersistenceManager.putBundle(AbstractBundlePersistenceManager.java:800) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.persistence.bundle.AbstractBundlePersistenceManager.storeInternal(AbstractBundlePersistenceManager.java:715) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.persistence.bundle.AbstractBundlePersistenceManager.store(AbstractBundlePersistenceManager.java:593) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.persistence.pool.BundleDbPersistenceManager.store(BundleDbPersistenceManager.java:482) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.state.SharedItemStateManager.createRootNodeState(SharedItemStateManager.java:1725) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.state.SharedItemStateManager.<init>(SharedItemStateManager.java:217) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.RepositoryImpl.createItemStateManager(RepositoryImpl.java:1396) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.RepositoryImpl$WorkspaceInfo.doInitialize(RepositoryImpl.java:2062) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.RepositoryImpl$WorkspaceInfo.initialize(RepositoryImpl.java:2035) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.RepositoryImpl.initStartupWorkspaces(RepositoryImpl.java:543) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.RepositoryImpl.<init>(RepositoryImpl.java:326) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.RepositoryImpl.create(RepositoryImpl.java:615) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.TransientRepository$3.getRepository(TransientRepository.java:250) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.TransientRepository.startRepository(TransientRepository.java:280) [jackrabbit-core-2.12.4.jar:2.12.4]
    at org.apache.jackrabbit.core.TransientRepository.login(TransientRepository.java:376) [jackrabbit-core-2.12.4.jar:2.12.4]

调试,我可以看到它最初检查此捆绑包 - System Root Node - 是否存在,并且确实发现它已经在数据库中,但是当尝试插入不在数据库中的 Root Node 时,然后它也尝试再次插入System Root Node,这就是它违反约束的地方。

这是我用来创建存储库实例的代码:

Map<String, String> props = new HashMap<>();

props.put(RepositoryFactoryImpl.REPOSITORY_HOME, "jackrabbit");
props.put(RepositoryFactoryImpl.REPOSITORY_CONF, "classpath:jackrabbit/repository.xml");

Repository repository =  JcrUtils.getRepository(props);

repository.login(new SimpleCredentials("admin", "admin".toCharArray()));

还有我的 repository.xml:

<FileSystem class="org.apache.jackrabbit.core.fs.db.OracleFileSystem">
    <param name="url" value="jdbc:oracle:thin:@mydb:1521:XE"/>
    <param name="user" value="myuser"/>
    <param name="password" value="mypassword"/>
    <param name="schemaObjectPrefix" value="REPO_"/>
    <param name="schemaCheckEnabled" value="false"/>
</FileSystem>

<DataStore class="org.apache.jackrabbit.core.data.db.DbDataStore">
    <param name="url" value="jdbc:oracle:thin:@mydb:1521:XE"/>
    <param name="user" value="myuser"/>
    <param name="password" value="mypassword"/>
    <param name="databaseType" value="oracle"/>
    <param name="driver" value="oracle.jdbc.driver.OracleDriver"/>
    <param name="schemaObjectPrefix" value="REPO_"/>
    <param name="schemaCheckEnabled" value="false"/>
</DataStore>

<Security appName="Jackrabbit">
    <SecurityManager class="org.apache.jackrabbit.core.security.simple.SimpleSecurityManager"
                     workspaceName="security"/>
    <AccessManager class="org.apache.jackrabbit.core.security.simple.SimpleAccessManager"/>
    <LoginModule class="org.apache.jackrabbit.core.security.simple.SimpleLoginModule"/>
</Security>

<Workspaces rootPath="$rep.home/workspaces" defaultWorkspace="ContentRepo"/>
<Workspace name="ContentRepo">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.OracleFileSystem">
        <param name="url" value="jdbc:oracle:thin:@mydb:1521:XE"/>
        <param name="user" value="myuser"/>
        <param name="password" value="mypassword"/>
        <param name="schemaObjectPrefix" value="REPO_"/>
        <param name="schemaCheckEnabled" value="false"/>
    </FileSystem>

    <PersistenceManager class="org.apache.jackrabbit.core.persistence.bundle.OraclePersistenceManager">
        <param name="url" value="jdbc:oracle:thin:@mydb:1521:XE"/>
        <param name="user" value="myuser"/>
        <param name="password" value="mypassword"/>
        <param name="schemaObjectPrefix" value="REPO_"/>
        <param name="schemaCheckEnabled" value="false"/>
    </PersistenceManager>

    <SearchIndex class="org.apache.jackrabbit.core.query.lucene.SearchIndex">
        <param name="path" value="$rep.home/index"/>
        <param name="textFilterClasses" value="org.apache.jackrabbit.extractor.PlainTextExtractor,org.apache.jackrabbit.extractor.MsWordTextExtractor,org.apache.jackrabbit.extractor.MsExcelTextExtractor,org.apache.jackrabbit.extractor.MsPowerPointTextExtractor,org.apache.jackrabbit.extractor.PdfTextExtractor,org.apache.jackrabbit.extractor.OpenOfficeTextExtractor,org.apache.jackrabbit.extractor.RTFTextExtractor,org.apache.jackrabbit.extractor.HTMLTextExtractor,org.apache.jackrabbit.extractor.XMLTextExtractor"/>
        <param name="extractorPoolSize" value="2"/>
        <param name="supportHighlighting" value="true"/>
    </SearchIndex>
</Workspace>

<Versioning rootPath="$rep.home/version">
    <FileSystem class="org.apache.jackrabbit.core.fs.db.OracleFileSystem">
        <param name="url" value="jdbc:oracle:thin:@mydb:1521:XE"/>
        <param name="user" value="myuser"/>
        <param name="password" value="mypassword"/>
        <param name="schemaObjectPrefix" value="REPO_"/>
        <param name="schemaCheckEnabled" value="false"/>
    </FileSystem>

    <PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.OraclePersistenceManager">
        <param name="url" value="jdbc:oracle:thin:@mydb:1521:XE"/>
        <param name="user" value="myuser"/>
        <param name="password" value="mypassword"/>
        <param name="schemaObjectPrefix" value="REPO_"/>
        <param name="schemaCheckEnabled" value="false"/>
    </PersistenceManager>
</Versioning>

<SearchIndex class="org.apache.jackrabbit.core.query.lucene.SearchIndex">
    <param name="path" value="$rep.home/repository/index"/>
    <param name="textFilterClasses" value="org.apache.jackrabbit.extractor.PdfTextExtractor,org.apache.jackrabbit.extractor.OpenOfficeTextExtractor,org.apache.jackrabbit.extractor.RTFTextExtractor,org.apache.jackrabbit.extractor.HTMLTextExtractor,org.apache.jackrabbit.extractor.XMLTextExtractor"/>
    <param name="extractorPoolSize" value="2"/>
    <param name="supportHighlighting" value="true"/>
</SearchIndex>

<Cluster>
    <Journal class="org.apache.jackrabbit.core.journal.OracleDatabaseJournal">
        <param name="url" value="jdbc:oracle:thin:@mydb:1521:XE"/>
        <param name="user" value="myuser"/>
        <param name="password" value="mypassword"/>
        <param name="schemaObjectPrefix" value="REPO_"/>
        <param name="schemaCheckEnabled" value="false"/>
    </Journal>
</Cluster>

版本: Oracle 11.2.0 Java 8 Apache Jackrabbit 2.12.4

感谢任何帮助。

【问题讨论】:

【参考方案1】:

找到了解决方法,但我确信我的配置仍然存在问题,而不是 Jackrabbit Oracle 支持中的错误。

无论如何,截止日期就是这样,我能够通过覆盖 OraclePersistenceManager.storeBundle 来让一切正常工作:

public class MyPersistenceManager extends OraclePersistenceManager 

    protected synchronized void storeBundle(NodePropBundle bundle) throws ItemStateException 
        if (exists(bundle.getId())) 
            bundle.markOld();
        
        super.storeBundle(bundle);
    


希望这不仅仅隐藏了一个在路上出现的问题。如果有,我会更新的。

【讨论】:

以上是关于oracle数据导入时,提示违反唯一约束性?的主要内容,如果未能解决你的问题,请参考以下文章

Oracle数据库中违反唯一约束的处理

ORA-02292: 违反完整约束条件 处理

ORA-00001: 违反唯一约束条件

Hibernate主键生成策略strategy = "increment"报错违反唯一性约束

违反唯一约束会导致整个 pq.CopyIn postgresql 导入失败

Oracle数据库主键约束与唯一索引有啥区别?