问题描述
有人可以帮我吗?
使用 dbunit-maven-plugin-1.0-beta-3 和 MysqL 8 将元数据加载到带有 mysql-connector-java-8.0.22 的数据库表中。
出现如下错误
org.apache.maven.lifecycle.LifecycleExecutionException: Error executing database operation: CLEAN_INSERT
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error executing database operation: CLEAN_INSERT
at org.codehaus.mojo.dbunit.OperationMojo.execute(OperationMojo.java:109)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: org.dbunit.database.AmbiguousTableNameException: USERTABLE
at org.dbunit.dataset.OrderedTableNameMap.add(OrderedTableNameMap.java:198)
at org.dbunit.database.DatabaseDataSet.initialize(DatabaseDataSet.java:227)
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:275)
at org.dbunit.operation.DeleteallOperation.execute(DeleteallOperation.java:109)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at org.dbunit.ant.Operation.execute(Operation.java:195)
at org.codehaus.mojo.dbunit.OperationMojo.execute(OperationMojo.java:100)
... 19 more
仅供参考,它适用于 MysqL-connoctor-java-5.1.49。但在我们的例子中,我们需要使用 MysqL 连接器 8.0.22。
谢谢。
解决方法
顺便说一句,我有两个表名相同的数据库。
开启 Qualified Table Names 功能是否可以解决问题?如果用户有多个模式,dbUnit 可能需要知道使用哪个模式。
例如,功能页面顶部有一个。还有一个使用 PrepAndExpectedTestCase 的完整示例。
总体思路是:
final IDatabaseTester databaseTester = new xxxx();
final IDatabaseConnection dbUnitConnection = databaseTester.getConnection();
final DatabaseConfig databaseConfig = dbUnitConnection.getConfig();
config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES,true);
,
这是 pom dbunit 配置的一部分,它为我修复了这个问题。
<schema>test_database</schema>
<dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName>
<metadataHandlerName>org.dbunit.ext.mysql.MySqlMetadataHandler</metadataHandlerName>