Reputation: 460
I have used vlad mihalcea's dependency to store json value in table.
Table name: valuation_report JsonbColumn is parameters attribute name in pojo is params
Able to get value of single object like address="Address1"
{"address":"Address1","nestedObj":{"firstName":"Sanjay"}}
To get address I've implemented specification and @Overriden Predicate method like
@Override
public Predicate toPredicate(Root<ValuationReport> root, CriteriaQuery<?> query, CriteriaBuilder cb)
{
return cb.equal(cb.function("jsonb_extract_path_text", String.class,root.<String>.get("params"),cb.literal(this.locale)), this.fieldToSearch);
}
But now i want to find value of NestedObj like nestedObj's firstName is Sanjay.
{"nestedObj":{"firstName":"Sanjay"}}
Please help me😅 And I've written all this with my mobile so sorry for bad format of question😅.
Upvotes: 5
Views: 3072
Reputation: 51
Here is your JPA specification.
public Specification<ValuationReport> getFirstNameSpecification(
final String param, final String subParam, final String value) {
return (root, query, cb) ->
cb.equal(
cb.function(
"jsonb_extract_path_text",
String.class,
root.get("params"),
cb.literal(param),
cb.literal(subParam)),
value);
}
Thanks
Upvotes: 4
Reputation: 137
This HQL query works the same way for me (for simple approach).
public void someFunction(String searchKey){
Query query="select obj FROM ValuationReport obj where jsonb_extract_path_text(obj.params,:subParam ,:key)=:value")
Query<ValuationReportJSON> parameters=session.createQuery(query).setParameter("value",searchKey);
List<ValuationReportJSON> resultset=parameters.getResultList();
}
Hope it helps !
Upvotes: 2
Reputation: 68
I have done this with @Query annotation but there's still a way to do with criteria also. I will show my query to do this
@Query(value = "select vr FROM ValuationReport vr where jsonb_extract_path_text(vr.params,:subParam ,:key)=:value")
List<ValuationReportJSON> getEntities(@Param("subParam") String subParam,@Param("key") String key,@Param("value") String value);
Hope this works.
Upvotes: 3