尝试加载 CLOB 字段时,DBunit 生成 java.lang.ClassCastException:java.lang.String 无法转换为 oracle.sql.CLOB

发布于 2024-08-24 19:47:37 字数 4679 浏览 11 评论 0原文

我在 Oracle 11GR2 上使用最新版本的 DBUnit (2.4.7)。我正在使用 Java 6 (1.6.0_15) 和最新版本的 Oracle 客户端 jar (jdbc6.jar)

我无法成功将 CLOB Oracle 字段引用的任何数据从 XML 文件加载到数据库中。

我使用过以下版本的各种组合:

  • Oracle JDBC 库 ojdbc5.jar、ojdbc6.jar、oracle 10 jar
  • Hibernate 库等...我认为问题出在 DBUnit 上。请参阅下面的堆栈跟踪。

    org.hibernate;
    hibernate-core;
    <版本>3.5.0-CR-2
    

    org.hibernate;
    hibernate-entitymanager;
    <版本>3.4.0.GA
    

正在运行 Java 6,1.6.0_15。

我尝试过:

1)使用此定义的 FlatXmlDataSet

<MESSAGE msg_id="1" mtp_id="1" msg_detail="asadds" />

2)使用此定义的 XmlDataSet

<table name="MESSAGE">
    <column>MSG_ID</column>
    <column>MTP_ID</column>
    <column>MSG_DETAIL</column>
    <row>
        <value>1</value>
        <value>1</value>
        <value>dsad</value>
    </row>
</table>

任何帮助将不胜感激!

堆栈跟踪如下:

        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:980)
ERROR [10032010 14:15:13,031] - exception creating EntityManager:  [] (MessageDAOTest.java:97)
java.lang.RuntimeException: Exception in JpaDBTestCase
        at com.ert.commons.junit4.hibernate.JpaDBTestCase.loadDbunitFiles(JpaDBTestCase.java:97)
        at com.ert.ertmon.dao.ejb.impl.MessageDAOTest.setUpBeforeClass(MessageDAOTest.java:94)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:334)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:980)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB
        at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:7898)
        at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:7511)
        at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:7984)
        at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:237)
        at org.dbunit.dataset.datatype.ClobDataType.setSqlValue(ClobDataType.java:71)
        at org.dbunit.database.statement.SimplePreparedStatement.addValue(SimplePreparedStatement.java:73)
        at org.dbunit.database.statement.AutomaticPreparedBatchStatement.addValue(AutomaticPreparedBatchStatement.java:63)
        at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:186)
        at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
        at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
        at com.ert.commons.junit4.hibernate.JpaDBTestCase.loadDbunitFile(JpaDBTestCase.java:136)
        at com.ert.commons.junit4.hibernate.JpaDBTestCase.loadDbunitFiles(JpaDBTestCase.java:92)
        ... 21 more

I am using the latest version of DBUnit (2.4.7), on Oracle 11GR2. I'm using Java 6 (1.6.0_15) and the latest version of Oracle's client jar (jdbc6.jar)

I've been unable to successfully load any data referenced by a CLOB Oracle field from an XML file into the database.

I've used all sorts of combinations of versions of the:

  • Oracle JDBC library ojdbc5.jar, ojdbc6.jar, oracle 10 jars
  • Hibernate library etc... I think the problem lies in DBUnit. See below for stack trace.

    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.5.0-CR-2</version>
    

    and

    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.4.0.GA</version>
    

I'm running Java 6, 1.6.0_15.

I've tried:

1) FlatXmlDataSet using this definition

<MESSAGE msg_id="1" mtp_id="1" msg_detail="asadds" />

2) XmlDataSet using this definition

<table name="MESSAGE">
    <column>MSG_ID</column>
    <column>MTP_ID</column>
    <column>MSG_DETAIL</column>
    <row>
        <value>1</value>
        <value>1</value>
        <value>dsad</value>
    </row>
</table>

Any help would be much appreciated!

Stack trace follows:

        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:980)
ERROR [10032010 14:15:13,031] - exception creating EntityManager:  [] (MessageDAOTest.java:97)
java.lang.RuntimeException: Exception in JpaDBTestCase
        at com.ert.commons.junit4.hibernate.JpaDBTestCase.loadDbunitFiles(JpaDBTestCase.java:97)
        at com.ert.ertmon.dao.ejb.impl.MessageDAOTest.setUpBeforeClass(MessageDAOTest.java:94)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:334)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:980)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB
        at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:7898)
        at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:7511)
        at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:7984)
        at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:237)
        at org.dbunit.dataset.datatype.ClobDataType.setSqlValue(ClobDataType.java:71)
        at org.dbunit.database.statement.SimplePreparedStatement.addValue(SimplePreparedStatement.java:73)
        at org.dbunit.database.statement.AutomaticPreparedBatchStatement.addValue(AutomaticPreparedBatchStatement.java:63)
        at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:186)
        at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
        at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
        at com.ert.commons.junit4.hibernate.JpaDBTestCase.loadDbunitFile(JpaDBTestCase.java:136)
        at com.ert.commons.junit4.hibernate.JpaDBTestCase.loadDbunitFiles(JpaDBTestCase.java:92)
        ... 21 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三寸金莲 2024-08-31 19:47:37

使用
数据工厂配置中的org.dbunit.ext.oracle.Oracle10DataTypeFactory

Use
org.dbunit.ext.oracle.Oracle10DataTypeFactory

in datafactory config.

洒一地阳光 2024-08-31 19:47:37

This looks like Bug ID 1984596 and I don't really understand the status (it is closed but... I don't get if the issue has been fixed). Could you try with DbUnit 2.2.1 as suggested in the issue (it seems that a change introduced in version 2.2.2 is causing the problem). If this works, you should definitely reopen the issue.

清风不识月 2024-08-31 19:47:37

前几天我在使用 Ant 和 DBUnit 时也遇到了这个问题。我正在使用 Ant 版本 1.7.1、DBUnit 2.4.5. 和 Oracle 10g。我发现的“解决方法”是在 Ant 任务中指定数据库方言:

<dbunit driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:[email protected]:1521:MyOracleDb" userid="[userid]" password="[password]" schema="[MySchema]">
    <dbconfig>
       <property name="datatypeFactory" value="org.dbunit.ext.oracle.OracleDataTypeFactory"/>
    </dbconfig>
    <operation type="CLEAN_INSERT" src="MY_DATA.xml" />
</dbunit>

添加 dbconfig 属性后,String / CLOB 转换问题就消失了。希望这对任何在 Ant 中遇到此错误的人有所帮助。

I also encountered this problem the other day using Ant and DBUnit. I'm using Ant version 1.7.1, DBUnit 2.4.5., with Oracle 10g. The 'work around' I found was to specify the database dialect in the Ant Task:

<dbunit driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:[email protected]:1521:MyOracleDb" userid="[userid]" password="[password]" schema="[MySchema]">
    <dbconfig>
       <property name="datatypeFactory" value="org.dbunit.ext.oracle.OracleDataTypeFactory"/>
    </dbconfig>
    <operation type="CLEAN_INSERT" src="MY_DATA.xml" />
</dbunit>

Once I added the dbconfig property, the String / CLOB cast issue was gone. Hope this helps anyone that has seen this error with Ant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文