Maxim Popravko
Maxim Popravko

Reputation: 4159

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.

Upvotes: 2

Views: 4342

Answers (2)

magicgone
magicgone

Reputation: 1

first get Object instead of byte[]

public Object getTypicalTaskMeasurementParameterValue(
    Integer typicalTaskMeasurementId
);

Then change Object to byte[].

Upvotes: -1

Andy
Andy

Reputation: 8949

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.

Upvotes: 2

Related Questions