简体   繁体   English

使用 Oracle RAW 类型的 JOOQ Where 子句

[英]JOOQ Where clause using Oracle RAW type

I'm trying to write a JOOQ query where it needs to search by UUID, which is stored in the Oracle database as RAW type.我正在尝试编写一个 JOOQ 查询,它需要通过 UUID 进行搜索,UUID 以 RAW 类型存储在 Oracle 数据库中。 In Jooq generated entities these type of fields are defined as byte array.在 Jooq 生成的实体中,这些类型的字段被定义为字节数组。

Simply converting the string to byte array does not find the matching value: jooqQuery.addConditions(TABLE.UUID_COLUMN.eq(UUID_AS_STRING).getBytes()));简单地将字符串转换为字节数组不会找到匹配的值: jooqQuery.addConditions(TABLE.UUID_COLUMN.eq(UUID_AS_STRING).getBytes()));

On the other hand when casting a column name to String:另一方面,将列名转换为字符串时:

jooqQuery.addConditions(TABLE.UUID_COLUMN.cast(String.class).eq(UUID_AS_STRING));

produces jdbc exception at runtime:在运行时产生 jdbc 异常:

"class":"ohengine.jdbc.spi.SqlExceptionHelper","rest":"ORA-00906: missing left parenthesis\\n"

Is there a right way to cast this to produce a valid sql that work properly?有没有正确的方法来转换它以生成一个可以正常工作的有效 sql?

EDIT: I have also tried using TABLE.UUID_COLUMN.getDataType().convert(UUID_AS_STRING) The resulting query is identical to just doing getBytes() with the same empty search result编辑:我也尝试过使用TABLE.UUID_COLUMN.getDataType().convert(UUID_AS_STRING)结果查询与使用相同的空搜索结果执行 getBytes() 相同

SOLVED: Using this method, converts UUID to correct byte array:已解决:使用此方法,将 UUID 转换为正确的字节数组:

    private byte[] getUUIDtoBytes(UUID devId) {
        byte[] uuidBytes = new byte[16];
        ByteBuffer.wrap(uuidBytes)
                .order(ByteOrder.BIG_ENDIAN)
                .putLong(devId.getMostSignificantBits())
                .putLong(devId.getLeastSignificantBits());
        return uuidBytes;
    }

I doubt you've stored your UUID as uuid.getBytes() .我怀疑您是否已将 UUID 存储为uuid.getBytes() Calling String.getBytes() will yield the unicode encoding of the string, which would be a wasteful way to store a UUID, which is already in hexadecimal format.调用String.getBytes()将产生字符串的 unicode 编码,这对于存储已经是十六进制格式的 UUID 来说是一种浪费的方式。 A more likely encoding is documented in this SO question here .更可能的编码记录在这个 SO question here 中

In any case, you should find out how your UUID value was stored and encoded, and reuse that encoding mechanism.无论如何,您应该找出您的 UUID 值是如何存储和编码的,并重用该编码机制。 With jOOQ, ideally, you would attach a data type binding or converter to your UUID_COLUMN , such that you will never have to think about this again in any jOOQ query.使用 jOOQ,理想情况下,您可以将数据类型绑定或转换器附加到您的UUID_COLUMN ,这样您就不必在任何 jOOQ 查询中再次考虑这一点。 More information about this here: https://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings更多关于这里的信息: https : //www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM