对JOOQ的DSL.val()使用自定义转换器 [英] Using custom converter for JOOQ's DSL.val()

查看:47
本文介绍了对JOOQ的DSL.val()使用自定义转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,使用java.util.UUID标识了一条内容.在数据库中存储信息时,我正在使用的相应MySQL数据类型为BINARY(16). BINARY的默认JDBC数据类型为byte[].因此,我有一个自定义的org.jooq.Converter在UUID和byte []之间进行转换.

In my application, a piece of content is identified using java.util.UUID. While storing information in the database, the corresponding MySQL data type that I am using is BINARY(16). The default JDBC data type for BINARY is byte[]. So I have a custom org.jooq.Converter to translate between UUID and byte[].

我有一个用例,在该用例中,我需要将表中的记录复制到同一表中,但只复制某些列而不是全部.在最初的问题中,我在此处发布了以下解决方案

I have a use-case in which I need to copy a record from a table into the same table but copy only certain columns and not all. In the original question I had posted here, The following was the solution

public void copy(UUID source, UUID target) {
    jooq.insertInto(PERSON)
            .columns(PERSON.ID, PERSON.FNAME, PERSON.LNAME)
            .select(select(val(target), PERSON.FNAME, PERSON.LNAME)
                   .from(PERSON)
                   .where(PERSON.ID.eq(source)))
            .execute();
} 

但是执行这段代码会导致异常

But executing this piece of code led to an exception

Data truncation: Data too long for column 'id' at row 1

我发现DSL.val()没有使用自定义转换器将UUID转换为byte [].如何强制DSL.val()函数使用自定义转换器?

I figured out that DSL.val() was not using the custom converter I had in place for converting UUID to byte[]. How can I force the DSL.val() function to use the custom converter?

推荐答案

解决方案:使用DSL.val(java.lang.Object value, DataType<T> type)而不是DSL.val(java.lang.Object value)

Solution: Use DSL.val(java.lang.Object value, DataType<T> type) instead of just DSL.val(java.lang.Object value)

以下是更新的代码段

public void copy(UUID source, UUID target) {
    DataType<UUID> uuidType = SQLDataType.BINARY
                                .asConvertedDataType(new UuidBinaryConverter());
    jooq.insertInto(PERSON)
            .columns(PERSON.ID, PERSON.FNAME, PERSON.LNAME)
            .select(select(val(target, uuidType), PERSON.FNAME, PERSON.LNAME)
                   .from(PERSON)
                   .where(PERSON.ID.eq(source)))
            .execute();
} 

这篇关于对JOOQ的DSL.val()使用自定义转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆