简体   繁体   中英

Dbunit gets AmbiguousTableNameException Oracle

I set up the persistence layer tests based on the DbUnit test framework (H2 database in memory) that work very well, but when I wanted to switch to an Oracle database I have this error:

org.dbunit.database.AmbiguousTableNameException: AQ$_SCHEDULES

at org.dbunit.dataset.OrderedTableNameMap.add(OrderedTableNameMap.java:198)
at org.dbunit.database.DatabaseDataSet.initialize(DatabaseDataSet.java:231)
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:281)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
at fr.pe.rind.service.dd016.dbunitoracle.ConfigDbUnitOracle.cleanlyInsert(ConfigDbUnitOracle.java:78)
at fr.pe.rind.service.dd016.dbunitoracle.ConfigDbUnitOracle.importDataSet(ConfigDbUnitOracle.java:64)
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:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

To test on the new Oracle base type, I copied the persistence.xml file into my test module (by changing the name of because I created a new source of data for these tests) Here is the new persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
        <persistence-unit name="DbUnitPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>DsOracleDbUnit</jta-data-source>
        <mapping-file>jpa/orm.xml</mapping-file>
        <properties>

            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="javax.persistence.jdbc.password" value="dbUnit"/>
            <property name="javax.persistence.jdbc.user" value="dbUnit"/>
            <!-- Hibernate properties -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.show_sql" value="true"/>

        </properties>

    </persistence-unit>

</persistence>

My class Test :

public class ConfigDbUnitOracle {

private static EntityManagerFactory entityManagerFactory;
protected static EntityManager entityManager;
private static IDatabaseConnection dbunitConnection;

private static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String JDBC_URL = "jdbc:oracle:thin:@localhost:1521:XE";
private static final String USER = "dbUnit";
private static final String PASSWORD = "dbUnit";


@BeforeClass
public static void createSchema() throws Exception {
    entityManagerFactory = Persistence.createEntityManagerFactory("DbUnitPU");
    entityManager = entityManagerFactory.createEntityManager();

    Connection connection = ((EntityManagerImpl) (entityManager.getDelegate())).getServerSession().getAccessor().getConnection();
    dbunitConnection = new DatabaseConnection(connection, "DBUNIT");
    DatabaseConfig dbCfg = dbunitConnection.getConfig();
    dbCfg.setProperty(DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, Boolean.TRUE);
    dbCfg.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, Boolean.TRUE);
    dbCfg.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new Oracle10DataTypeFactory());
}

@Before
public void importDataSet() throws Exception {
    IDataSet dataSet = readDataSet();
    cleanlyInsert(dataSet);
}

private void cleanlyInsert(IDataSet dataSet) throws Exception {
    IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
    //CLEAN_INSERT on demande à DbUnit d supprimer toutes les lignes, puis insérer celles de dataset
    databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
    databaseTester.setDataSet(dataSet);
    databaseTester.onSetup();//Error org.dbunit.database.AmbiguousTableNameException
}

private IDataSet readDataSet() throws Exception {
    return new FlatXmlDataSetBuilder().build(new File(ConstanteDbUnit.DATASET_CHEMIN));
}


@AfterClass
public static void closeEntityManager() throws Exception {
    entityManager.clear();
    entityManager.close();
    entityManagerFactory.close();
}

@Test
public void myTest() {
    //my Test
}

}

I found solutions for the same problem but none worked for me!

This appears to be a DbUnit FAQ.

You can read the answer here .

Here is an excerpt.

Why am I getting an "AmbiguousTableNameException"?
This error occurs when no schema is specified and that DbUnit detect that it is getting columns information from multiple tables having the same name and located in different schemas.

The FAQ also provides suggestions for resolving the problem.

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM