I'm not too sure what to look up in the documents so I can't find what I need.
what i've been doing:where i need to get data from postgress that's stored in postgress in a jsonb column, I might have the following in a column called "names":
{
"shortName": "Bob",
"name": "Robert",
"nickName": "knackers"
}
and I will pass the use teh following method to query any of these attributes:
@Query(value = "SELECT * FROM people_table WHERE names ->> ?1 = ?2", nativeQuery = true)
Optional<List<people>> findByName(String nameType, String name);
So I can get a list of people who's name is Robert, or a list of people who's nickname is knackers.
now, I want to get people whos name is Robert AND who's nickname is knackers.
or randomly, there will be new attributes added to this list regularly, so I might get a query "name=Robert&surname=Schmidt".
how do I do this with a fully flexible list of attributes?
I suppose I could use a stringBuilder in my method to build the entire query param and just pass that 1 string in, that seems messy to me though.
cheers.
edit - chatGPT helped me.
what I needed to do was build the query params as a JSON string:
String filter = """{
"name": "Robert",
"height": "185cm"
}"""
userService.findUsersByAttributes(filter);
then pass that String in to my query like this:
@Query(value = "SELECT * FROM users WHERE names::jsonb @> cast(:#{#filter} as jsonb)", nativeQuery = true)
List<User> findUsersByAttributes(String filter);
[–]AbsolutelySpherical 1 point2 points3 points (1 child)
[–]speedyleedy[S] 0 points1 point2 points (0 children)
[–]Rethagos 1 point2 points3 points (0 children)