How to insert bytea using annotations?

101 views
Skip to first unread message

Miguel Silvestre

unread,
Mar 9, 2021, 4:35:28 AM3/9/21
to jDBI
Hi community,

I'm migrating from hibernate to jDBI. However I'm struggling on how to achieve this.
I have a bytea type on my postgres table. How can I bind an object to it?

The schema of the table is:
create table service_status_progress (
   id varchar(255) not null,
   error bytea,
   service jsonb not null,
   status varchar(255) not null,
   primary key (id)
);


And I have the DAO:

import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

public interface ServiceStatusProgressDao {
    @SqlUpdate("INSERT INTO service_status_progress (id, error, service, status) "
            + "VALUES (:id, :error, :service, :status")
    void insert(@Bind("id") String id,
                @Bind("error") byte[] error,
                @Bind("service") String service,
                @Bind("status") ServiceStatusProgress.StatusEnum status);
}

The error is actualy a Throwable that I'm serialising has byte[]. But it is not working. 
I think this is not the way of doing this. But I'm not getting any examples on internet on about how to do this.

Thank you
Message has been deleted

Matthew Hall

unread,
Mar 9, 2021, 12:59:17 PM3/9/21
to jd...@googlegroups.com
Looks like we're not setting a default for the config option PostgresTypes.lobApi, and the default implementation class is package private--would you open a GH issue?

-Matthew

On Tue, Mar 9, 2021 at 4:06 AM Miguel Silvestre <msilv...@gmail.com> wrote:
Ok, so looking into documentation (RTFM) I can see the following:

So it should be an InputStream not a byte[].

So I have this code converting throwable to InputStream:
    private InputStream toInputStream(Throwable throwable) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(throwable);
            oos.flush();
            oos.close();
            return new ByteArrayInputStream(bos.toByteArray());
        } catch (IOException e) {
            e.printStackTrace(); //TODO
            return null;
        }
    }

And my Dao has:
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

import java.io.InputStream;

public interface ServiceStatusProgressDao {
    @SqlUpdate("INSERT INTO service_status_progress (id, error, service, status) "
            + "VALUES (?, ?, ?, ?)")
    void insert(String id,
                InputStream error,
                String service,
                ServiceStatusProgress.StatusEnum status);
}

It is called like this:
Override
    public void put(ServiceStatusProgress serviceStatusProgress) {
        serviceStatusProgressDao.insert(
                serviceStatusProgress.getId(),
                toInputStream(serviceStatusProgress.getError()),
                JsonConverterUtil.toJson(serviceStatusProgress.getService(), objectMapper),
                serviceStatusProgress.getStatus());
    }

However it is throwing NPE:
java.lang.NullPointerException
at org.jdbi.v3.postgres.BlobInputStreamArgumentFactory$LobInputStreamArgument.apply(BlobInputStreamArgumentFactory.java:46)
at org.jdbi.v3.core.statement.DescribedArgument.apply(DescribedArgument.java:43)
at org.jdbi.v3.core.statement.ArgumentBinder.bindPositional(ArgumentBinder.java:74)
at org.jdbi.v3.core.statement.ArgumentBinder.bind(ArgumentBinder.java:58)
at org.jdbi.v3.core.statement.SqlStatement.internalExecute(SqlStatement.java:1781)
at org.jdbi.v3.core.result.ResultProducers.lambda$returningUpdateCount$0(ResultProducers.java:46)
at org.jdbi.v3.core.statement.Update.execute(Update.java:60)
at org.jdbi.v3.core.statement.Update.execute(Update.java:48)
at org.jdbi.v3.sqlobject.statement.internal.SqlUpdateHandler.lambda$new$1(SqlUpdateHandler.java:65)
at org.jdbi.v3.sqlobject.statement.internal.SqlUpdateHandler.lambda$configureReturner$3(SqlUpdateHandler.java:90)
at org.jdbi.v3.sqlobject.statement.internal.CustomizingStatementHandler.invoke(CustomizingStatementHandler.java:178)
at org.jdbi.v3.sqlobject.statement.internal.SqlUpdateHandler.invoke(SqlUpdateHandler.java:32)
at org.jdbi.v3.sqlobject.internal.SqlObjectInitData$1.lambda$invoke$0(SqlObjectInitData.java:132)
at org.jdbi.v3.core.internal.Invocations.invokeWith(Invocations.java:44)
at org.jdbi.v3.core.internal.Invocations.invokeWith(Invocations.java:26)
at org.jdbi.v3.core.LazyHandleSupplier.lambda$invokeInContext$1(LazyHandleSupplier.java:77)
at org.jdbi.v3.core.internal.Invocations.invokeWith(Invocations.java:44)
at org.jdbi.v3.core.internal.Invocations.invokeWith(Invocations.java:26)
at org.jdbi.v3.core.LazyHandleSupplier.invokeInContext(LazyHandleSupplier.java:76)
at org.jdbi.v3.sqlobject.internal.SqlObjectInitData$1.call(SqlObjectInitData.java:138)
at org.jdbi.v3.sqlobject.internal.SqlObjectInitData$1.invoke(SqlObjectInitData.java:132)
at org.jdbi.v3.sqlobject.SqlObjectFactory.lambda$attach$2(SqlObjectFactory.java:110)
at org.jdbi.v3.core.internal.OnDemandExtensions.lambda$invoke$5(OnDemandExtensions.java:98)
at org.jdbi.v3.core.internal.exceptions.Unchecked.lambda$function$4(Unchecked.java:76)
at org.jdbi.v3.core.internal.OnDemandExtensions.invoke(OnDemandExtensions.java:98)
at org.jdbi.v3.core.internal.OnDemandExtensions.lambda$null$2(OnDemandExtensions.java:82)
at org.jdbi.v3.core.Jdbi.callWithExtension(Jdbi.java:475)
at org.jdbi.v3.core.Jdbi.withExtension(Jdbi.java:462)
at org.jdbi.v3.core.internal.OnDemandExtensions.lambda$createProxy$3(OnDemandExtensions.java:82)
at com.sun.proxy.$Proxy161.insert(Unknown Source)
at com.xxx.xxx.xxx.hub.PersistentRdsHub.put(PersistentRdsHub.java:124)
at com.xxx.xxx.xxx.hub.PersistentRdsHub$$FastClassBySpringCGLIB$$9662d790.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
at com.xxx.xxx.xxx.hub.PersistentRdsHub$$EnhancerBySpringCGLIB$$7c1bae51.put(<generated>)
at com.xxx.xxx.xxx.hub.PersistentRdsHubIt.itShouldCreateServiceStatusProgress(PersistentRdsHubIt.java:243)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)


--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jdbi/c092577e-6421-4f6c-b47e-c09246b7c7efn%40googlegroups.com.

Steven Schlansker

unread,
Mar 9, 2021, 1:03:27 PM3/9/21
to jd...@googlegroups.com
I think byte[] binding should be supported as well - try using the @SingleValue annotation, then Jdbi will treat the byte[] as one byte array rather than an array of individual bytes.

Miguel Silvestre

unread,
Mar 12, 2021, 3:58:23 AM3/12/21
to jDBI
Hi thank you for all the support.

Meanwhile I has able to do it using byte[].
The previous  code had an error:

import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

public interface ServiceStatusProgressDao {
    @SqlUpdate("INSERT INTO service_status_progress (id, error, service, status) "
            + "VALUES (:id, :error, :service, :status)")
    void insert(@Bind("id") String id,
                @Bind("error") byte[] error,
                @Bind("service") String service,
                @Bind("status") ServiceStatusProgress.StatusEnum status);
}

The bold parenthesis was missing. I did not notice it :facepalm

Sorry, for any inconvenience.

Reply all
Reply to author
Forward
0 new messages