DBunit generates java.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB when trying to load a CLOB field
Asked Answered
C

3

9

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
Commutate answered 10/3, 2010 at 19:25 Comment(0)
D
4

Use org.dbunit.ext.oracle.Oracle10DataTypeFactory

in datafactory config.

Downtrend answered 2/9, 2012 at 8:59 Comment(1)
That's the right answer but I encountered the issue with NCLOB column. That's because of dbunit is still missing the 'nclob' case in Oracle10DataTypeFactory. After manually adding 'nclob' case into createDataType()(similarly to 'clob' one) method NCLOB columns started working correctly as well. It seems dbunit is fading away, very disappointing.Antipodal
W
3

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.

Wolver answered 10/3, 2010 at 22:49 Comment(5)
Wow, thanks for the hint, it actually worked. I actually saw that page in my travels but kind of rejected it due to its age and the DBunit version referenced. Just FYI, I needed to revert the org.dbunit.ext.oracle.Oracle10DataTypeFactory back to a org.dbunit.ext.oracle.OracleDataTypeFactory (and make other changes) but now Clobs successfully load in 11G. I still can't believe I'm the only one whose hit upon this (CLOB loading in Oracle 11G) I will update the above listed BUG ID as soon as my SourceForge id comes in the mail. Again, a really big thanks!Commutate
updated, lets see what comes of it ... doesn't seem to be much activity on DBUnit these days unfortunatelyCommutate
@Commutate Thank you very much (for the feedback and the effort). And yes, this very unfortunate (I like DbUnit very much personally).Wolver
One further update ... since bug 1984596 is marked as closed, I opened a new bug to see if this can get fixed (again) sourceforge.net/tracker/…Commutate
@Commutate Great (and not resurrecting that old bug is indeed better since versions changed etc). Thank you again.Wolver
M
3

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.

Mathews answered 29/10, 2010 at 12:51 Comment(1)
I can verify that setting the datatypeFactory works. We are using 2.4.8 and had the same problem, but specifying the Oracle10DatatypeFactory did the trick. We did it programmatically however, as in the following example: IDatabaseConnection conn = new DatabaseConnection(pConnection); DatabaseConfig config = conn.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new org.dbunit.ext.oracle.Oracle10DataTypeFactory()); Estell

© 2022 - 2024 — McMap. All rights reserved.