gpt4 book ai didi

java - Dbunit 获得 AmbigouslyTableNameException Oracle

转载 作者:行者123 更新时间:2023-12-02 11:42:18 24 4
gpt4 key购买 nike

我基于DbUnit测试框架(内存中的H2数据库)设置了持久层测试,效果很好,但是当我想切换到Oracle数据库时我有这个错误:

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)

为了测试新的 Oracle 基本类型,我将 persistence.xml 文件复制到我的测试模块中(通过更改名称,因为我为这些测试创建了新的数据源)这是新的 persistence.xml 文件:

<?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>

我的类(class)测试:

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
}

}

我找到了相同问题的解决方案,但没有一个对我有用!

最佳答案

这似乎是 DbUnit 常见问题解答。

您可以阅读答案here .

这里是摘录。

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.

常见问题解答还提供了解决问题的建议。

祝你好运!

关于java - Dbunit 获得 AmbigouslyTableNameException Oracle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48464166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com