5

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😅.

asked Feb 11, 2020 at 6:06
1
  • I'm new to stack overflow guys please help😅 Commented Feb 11, 2020 at 6:24

3 Answers 3

4

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

answered Jan 20, 2021 at 3:43
Sign up to request clarification or add additional context in comments.

Comments

3

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.

answered Feb 12, 2020 at 4:30

2 Comments

Thanks man this query worked. Im still eager to do the same using JPA or something
is the jsonb_extract_path_text available for other database?
2

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 !

answered Feb 12, 2020 at 4:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.