简体   繁体   中英

How to retrieve data from database using jOOQ based on jsontype properties

we have a Employee table with json type address column like mentioned below

Employee Table:

在此处输入图像描述

How to retrieve the Employee details based on city name which is in address column using jOOQ?

You can use the built-in JSON_VALUE() support of jOOQ:

ctx.selectFrom(EMPLOYEE)
   .where(cast(jsonValue(EMPLOYEE.ADDRESS, "$.city"), VARCHAR).eq("Mumbai"))
   .fetch();

This is assuming you use code generation and have the usual static imports:

import static org.jooq.impl.DSL.*;
import static org.jooq.impl.SQLDataType.*;

If you need support for any other vendor-specific JSON function that jOOQ doesn't support (yet), use plain SQL templating instead.

However, that approach is unlikely to take advantage of indexes, so if you can, I suggest normalising this data.

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