Skip to content

Instantly share code, notes, and snippets.

@dotJoel
Last active January 23, 2025 22:39
Show Gist options
  • Save dotJoel/f7d8445b538bf1a2bec3556f3f4f8d97 to your computer and use it in GitHub Desktop.
Save dotJoel/f7d8445b538bf1a2bec3556f3f4f8d97 to your computer and use it in GitHub Desktop.
Using JPA, here is how I created a predicate that searches a JSONB column for any of the provided keys.
builder.equal(
builder.function(
"JSONB_EXISTS_ANY",
Boolean.class,
root.get("nameOfJsonbColumnToSearch"),
builder.function(
"string_to_array",
String.class,
builder.literal(String.join(",", setOfValuesToSearchFor)),
builder.literal(",")
)
),
true
)
@dotJoel
Copy link
Author

dotJoel commented Jan 23, 2025

/shrug

doesn't seem proper in retrospect. i mean, it works, but the point of JSONB is to be able to efficiently get keys. i couldn't find a way to do it at the time, hence this hack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment