Search code examples
javapostgresqlibatismybatis

How can I get byte[] from bytea column with MyBatis?


I tried the following way.

Here is the query mapping:

<select id="getTypicalTaskMeasurementParameterValue" 
            parameterType="Integer" 
            resultType="byte[]">
    SELECT value 
    FROM typical_task_measurements_parameter_values 
    WHERE id_typical_task_measurement = #{typicalTaskMeasurementId}
</select>

Here is the method:

public byte[] getTypicalTaskMeasurementParameterValue(
    Integer typicalTaskMeasurementId);

And here is the error I got, trying to run the unit test against it:

nested exception is org.apache.ibatis.reflection.ReflectionException: 
Error instantiating class [Ljava.lang.Byte; with invalid types () or values (). 
Cause: java.lang.NoSuchMethodException: [Ljava.lang.Byte;.<init>()
at ...

Moreover, setter method for this bytea staff is ok.


Solution

  • The error message says the problem pretty well. There is no default constructor on java.lang.Byte.

    You need a result map that will choose which constructor to use, or implement your own TypeHandler.