org.dbunit.database.AmbiguousTableNameException with MySQL 8

64jmpszr  于 2022-12-22  发布在  Mysql
关注(0)|答案(3)|浏览(172)

can anybody help me with this?
using dbunit-maven-plugin-1.0-beta-3 with mysql 8 to load meta data to database tables with mysql-connector-java-8.0.22.
Got the error as follows

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

Btw, i have two databases with the same Table name.
FYI, it will work fine with mysql-connoctor-java-5.1.49. But in our case we need to use mysql connector 8.0.22.
Thanks.

yjghlzjz

yjghlzjz1#

Here's the piece of pom dbunit configuration that makes this one fixed for me.

<schema>test_database</schema>
<dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName>                    
<metadataHandlerName>org.dbunit.ext.mysql.MySqlMetadataHandler</metadataHandlerName>
kx7yvsdv

kx7yvsdv2#

Btw, i have two databases with the same Table name.
Does turning on Qualified Table Names feature fix the issue? dbUnit may need to know which schema to use if the user has multiple.
For examples, there's one at the top of the feature page. There's also a full example using PrepAndExpectedTestCase.
The general idea is:

final IDatabaseTester databaseTester = new xxxx();
final IDatabaseConnection dbUnitConnection = databaseTester.getConnection();
final DatabaseConfig databaseConfig = dbUnitConnection.getConfig();
config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
xfyts7mz

xfyts7mz3#

Here is my solution to the problem, in annotation format:

@DBUnit(schema = "test", qualifiedTableNames=true, dataTypeFactoryClass = MySqlDataTypeFactory.class, metaDataHandler= MySqlMetadataHandler.class )

You need to specify the schema and also dataTypeFactoryClass and metaDataHandler, if you have issues with MariaDB...

相关问题