简体   繁体   中英

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. In Jooq generated entities these type of fields are defined as byte array.

Simply converting the string to byte array does not find the matching value: 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:

"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?

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

SOLVED: Using this method, converts UUID to correct byte array:

    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() . 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. A more likely encoding is documented in this SO question here .

In any case, you should find out how your UUID value was stored and encoded, and reuse that encoding mechanism. 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. More information about this here: https://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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