QueryDsl in JSONB column postgresql - postgresql

I have an object which has jsonb column. Is there any way to query into this jsonb column using querydsl?
Here is an example of my objects:
public class Person {
#Type(type = "jsonb")
#Column(columnDefinition = "jsonb")
private PersonDocument personDocument;
}
public class PersonDocument {
private int documentType;
private List<DocumentDetail> documentDetails;
}
public class DocumentDetail {
private String country;
private String documentId;
private LocalDate expiryDate
}
I would like to search person by documentId, so I need to get to documentId column

QueryDSL doesn't have any support for queries on PostgreSQL jsonb column. I have one solution that works for Query on the PostgreSQL field using QueryDSL.
solution steps:
Create a database view using the table where jsonb column. The view uses to convert your jsonb to the table normal table structure. like table A(j jsonb) now you want the field "xyz" from "j" jsonb column from A table. you can apply the following query:
CREATE OR REPLACE VIEW view_b as select a.id as a_id,a->> "xyz" as xyz from A a
now, view table structure like view_b(a_id,xyz)
Now you create a domain object which represents this view.
#Table(name="view_b") public class ViewB{...}
now you can query using query DSL. like qViewB.xyz.eq("value")

Related

Spring Data JPA Select Distinct dynamic columns based on list of string input

I want to select columns based on my input (List < String >)
#Entity
#Table
public class Product {
#Id
private Long id;
private String name;
private String price;
}
I understand that to select distinct specific columns, I can use #Query("SELECT DISTINCT name FROM TABLE")
However, I want to give users the flexibility to select the columns they want. e.g. List < String > columns = Arrays.asList(["name", "price"]). This will then select distinct from both name and price columns.
For this create a custom method implementation and use one of the following (or of the many variants thereof)
construct a SQL or JPQL query using String concatenation (be careful not to introduce SQL injection vulnerabilities).
use some kind of criteria API like
JPA Criteria API
Querydsl
JOOQ

How to get db column value from Mybatis for sharding?

I use Mybatis to access db, and some tables is sharding by id with hash algorithm.
I want to write a Mybatis intecepter to change table name automatic, it need to get the sharding column value.
Table Entity:
#Data
#TableName("m_user")
public class User {
#TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
}
UserMapper sql:
#Select("select * from m_user where id = #{id2} and name = #{name2};")
List<User> selectByIdAndName(Integer id2, String name2);
I use boundSql.getParameterObject() and boundSql.getParameterMappings() to check, but I can not make sure whether the sharding column id is in sql and then get the value of the sharding column.
ParameterMappings values and ParameterObject values are here:
parameter mapping:ParameterMapping{property='id2', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}
parameter mapping:ParameterMapping{property='name2', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}
params:{id2=1, param1=1, name2=name1, param2=name1}
The parameters are Mapper function parameters, but I need sharding column id and value, program can only get id2 or param1.
How to get the db column and value from Mybatis?

Select Map<Integer, Integer> using mybatis

I want to select Map for each record. Result class looks like
public class Mapping {
private String name;
private Map<Integer, Integer>;
}
SQL table has only three columns namely name, id, partner_id.
How can I create Map of Id to Partner Id for each name using mybatis?
you can use sqlSession.selectForMap, and give mybatis the column which will be processed as the key of map , such as name. then it will return
Map<String,Map<String,Object>>
as a result, but the value is Map, key is the column name, you need to transform it .

JPA Table for Instanzvariable

I have this entity
#Entity
public class Measurement implements Serializable {
#ID
Long id;
double value;
String meter;
}
I would like to create an own table for each "Meter"(One per different value of the instanzvariable) . Is there Way to realise this with JAP?
Why even create a table for each object? Do you understand the meaning of relational databases and how to map an object?
What you need for this is a table "measurements" with columns id, value, meter.Each row will be a new set of values.
If you want to distinct "meters" then create a table "meter" , put values there and use foreign keys from table "measurements".
Maybe you should read something like this "http://www.ntu.edu.sg/home/ehchua/programming/sql/Relational_Database_Design.html"

named native query and mapping columns to field of entity that doesn't exist in table

I have a named native query and I am trying to map it to the return results of the named native query. There is a field that I want to add to my entity that doesn't exist in the table, but it will exist in the return result of the query. I guess this would be the same with a stored proc...
How do you map the return results of a stored proc in JPA?...
How do you even call a stored proc?
here is an example query of what I would like to do...
select d.list_id as LIST_ID, 0 as Parent_ID, d.description from EPCD13.distribution_list d
The Result will be mapped to this entity...
public class DistributionList implements Serializable {
#Id
#Column(name="LIST_ID")
private long listId;
private String description;
private String owner;
private String flag;
#Column(name="PARENT_ID", nullable = true)
private long parentID;
}
parent ID is not in any table in my database. I will also need to use this entity again for other calls, that have nothing to do with this call, and that will not need this parent_id? Is there anything in the JPA standard that will help me out?
If results from database are not required for further manipulation, just for preview, you can consider using database view or result classes constructor expression.
If entities retrieved from database are required for further manipulation, you can make use of multiple select expression and transient fields.
Replace #Column annotation with #Transient annotation over parentID.
After retrieving multiple columns from database, iterate over results and manually set parentID.