I have a service that processes user data. We are batch testing against PostgreSQL native query for performance, but are having trouble storing JSON values the way we anticipate. The JSON value is being passed to the native query, but the query is processing and storing the JSON data improperly.
The query is currently storing the JSON data into our PostgreSQL table as such:
"{\"user_id\":\"12345\",\"user_name\":\"test_name\",\"email\":\"test#test.com\"}"
This is our native query:
#Transactional
#Modifying
#Query(value = "INSERT INTO USER_DATA_TEST (USER_DATA) VALUES to_json(:#{#userData.userData})",
nativeQuery = true)
public void saveUserData(#Param("userData") UserData userData);
We would like our data to be stored properly in JSON format, such as:
{"user_id": "12345", "user_name": "test_name", "email": "test#test.com"}
Related
I'm trying migrate from postgres db to mongodb. I've a field JSON in postgres table, which has key and value pairs. I need to fetch the value based on JSON key and convert those values(values are basically postgres id) to Mongo objectID and then map those to mongo document.
JSON field looks like this
"B":["956c5b0a5341d14c23ceed071bf136f8"]}
I've written a function in java to convert postgres ID column to mongoID.
public String convertIDToMongo(String colName){
---encoding logic
}
This is working when the field is explicitly available in the table and also if the field datatype is not an array. In case of JSON and array, is there a way to fetch set of values from JSON field and convert them into Mongo Object ID?
"Select json::json->'A' from table" ```
gives me the value ["06992a6fef0bcfc1ede998ac7da8f08b","7d1d55e1078191e53244c41d6b55b3a3","31571835c6600517f4e4c15b5144959b"] But I need some help to convert this list of IDs to Mongo Object IDs. Below is how I convert for non JSON field with one postgres ID value to Mongo ID.
"Select " + convertIDToMongo('colName') + " as "_id", \n" +
Any help is greatly appreciated. Thanks.
You need to parse the JSON array, then call your converter for each item in the array. There are many JSON parsing options available.
I need to write a query in JPA where it can fetch all the passed values in the parameter(list) in the DB and return the data.
With the help of this post - How to escape question mark (?) character with Spring JpaRepository
I was able to convert the list to array and make the query something like this
select * from dashboards d WHERE jsonb_exists_any(d.roles_access, array['Role1']);
it works fine in pgAdmin, however in jpa it cant cast the parameter userRoles
#Query(value = "SELECT * FROM dashboards d WHERE jsonb_exists_any(d.roles_access, array[:userRoles])", nativeQuery = true)
List<Dashboard> findAllDashboardsByUserRole(#Param("userRoles") final String [] userRoles);
I have tried many ways like array[CAST(:userRoles to jsonb)] but nothing works.
Any suggestion on how to properly cast it would be appreciated
I have an n API using Spring Boot to return the data back from my MySQL db.
I would like to send in a parameter (to keep it simple as part of the URI) to only return an x amount of records back.
My question is
Is it easier to just return all the records back in the Spring Boot app and then only loop through al the records and return the x amount of records back via an Arraylist or
Is there an actual method I can call with either JPA or the standard super class CRUD from Java to get the correct result?
You can use native query in your repository.
For example you have controller named fetch_data_controller and a repository name fetch_data_repository and a table name fetch_data_table from where you have to fetch only specific data.
In fetch_data_repository write the query as follows:
#Query(value = "SELECT col_1,col_2 FROM fetch_data_table WHERE validation = 1", nativeQuery = true)
List<Map<String,String>> fetch_data_func();
In fetch_data_controller write code as follows:
List<Map<String,String>> fetched_data = fetch_data_repository.fetch_data_func();
I have created a table with following sql query in database.
CREATE TABLE Employees (
id int PRIMARY KEY,
name VARCHAR (100),
contact TEXT []
);
Now i populate it with some values.
INSERT INTO Employees
VALUES
(
1,
'Alice John',
ARRAY [ '132','567' ]
);
Now my question is how do i write query using java to retrieve
the employee record where contact is '567' ?
Note: I'm using jooq with postgresql database to perform all query operations.
jOOQ doesn't support too many vendor specific operations out of the box. In most cases, you are expected to extend jOOQ using plain SQL templating. In this case:
ctx.selectFrom(EMPLOYEES)
.where("{0} #> ARRAY[{1}]", EMPLOYEES.CONTACT, val("567"))
.fetch();
I'm using PostgreSQL database and having a JSON type column in a table. The JSON value which is stored there in the column is having an iterative section which i do need to be retrieved from the database as an list.
#Query(value = "SELECT json_array_elements(table_column_name->'BBList') FROM table_name", nativeQuery = true)
public List <Object[]> findJsonList();
And the query returns this error:
org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
What would be the correct return type of this?
(
In this case , try using ->> operator:
Please see post below :
Execute Query based on the JSON that stored inside the column