Buscar este blog

domingo, 1 de noviembre de 2015

org.hsqldb.HsqlException: data exception: string data, right truncation

This exception appear when you try to populate a database with DBUNIT, and there is one text field which content bigger than the column size. (I spent almost 30 minutes until I realized it...)

The solution is to increase the column size in the entity mapping.

As you can see, I had a column named T_EXPED with length 2, and I tried to put in it a string with lenght 3.

Entity mapping:
@Entity
@Table(name="INDDO")
public class Inddo {
 (...)
 
 @Column(name="T_EXPED", columnDefinition="char(2)")
 private String tipoExpediente;
 
 (...)
}

DBUNIT file:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
 <INDDO (...) T_EXPED="22I" (...) />
</dataset>

Full exception trace:
org.dbunit.DatabaseUnitException: Exception processing table name='INDDO'
 at org.dbunit.operation.RefreshOperation.execute(RefreshOperation.java:130)
 at es.cixtec.entradadocumentos.persistencia.facade.TestIndiceFacade.setup(TestIndiceFacade.java:69)
 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:601)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
 at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
 at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
 at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
 at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
 at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.sql.SQLDataException: data exception: string data, right truncation
 at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
 at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
 at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
 at org.hsqldb.jdbc.JDBCPreparedStatement.execute(Unknown Source)
 at org.dbunit.database.statement.SimplePreparedStatement.addBatch(SimplePreparedStatement.java:80)
 at org.dbunit.operation.RefreshOperation$RowOperation.execute(RefreshOperation.java:192)
 at org.dbunit.operation.RefreshOperation$InsertRowOperation.execute(RefreshOperation.java:258)
 at org.dbunit.operation.RefreshOperation.execute(RefreshOperation.java:115)
 ... 30 more
Caused by: org.hsqldb.HsqlException: data exception: string data, right truncation
 at org.hsqldb.error.Error.error(Unknown Source)
 at org.hsqldb.error.Error.error(Unknown Source)
 at org.hsqldb.types.CharacterType.castOrConvertToType(Unknown Source)
 at org.hsqldb.types.CharacterType.convertToType(Unknown Source)
 at org.hsqldb.StatementDML.getInsertData(Unknown Source)
 at org.hsqldb.StatementInsert.getResult(Unknown Source)
 at org.hsqldb.StatementDMQL.execute(Unknown Source)
 at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
 at org.hsqldb.Session.execute(Unknown Source)
 ... 36 more

1 comentario:

  1. It worked here.

    I didn't know that columnDefinition at Collum annotation allows define the length of collumn, I always use length instead.


    Thanks!

    ResponderEliminar