I have a table casemessage
and has following columns. And I am trying to search/query JSON
column using Spring Framework JPA
..
Here the status
column stores list of JSON
strings. For example:
1. [{"user_id": 1, "status": "sent"}, {"user_id": 2, "status": "delete"}]
2. [{"user_id": 3, "status": "delete"}, {"user_id": 2, "status": "sent"},{"user_id": 1, "status": "received"}]
3. [{"user_id": 1, "status": "received"}, {"user_id": 2, "status": "sent"}]
4. [{"user_id": 1, "status": "delete"}, {"user_id": 3, "status": "sent"}]
I am trying to query the casemessage
table to get all the rows where user_id
is 1
and status
is not delete
Using MySQL
query, I am able to query the table and get back expected results.
Here is the query, which I tried:
select * from casemessage where case_Id=1 and id not in(select id from cwot.casemessage where json_contains(status, '{"status" :"delete"}') and json_contains(status, '{"user_id" : 1}'));
When I tried this using Spring Framework JPA
(Spring Boot
), I got back an exception when running the application. Here is the statement that I tied:
@Query("select c from CaseMessage c where c.caseId=?1 and c.id not in(select cm.id from CaseMessage cm where json_contains(status, '{\"status\": \"delete\"}') and json_contains(status, '{\"user_id\": ?2}'))")
List<CaseMessageResponse> getAllCaseMessages(long caseId, long userId);
The error that I am getting back is:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: ( near line 1, column 172 [select c from com.cwot.domain.CaseMessage c where c.caseId=?1 and c.id not in(select cm.id from com.cwot.domain.CaseMessage cm where json_contains(status, '{"status": "delete"}') and json_contains(status, '{"user_id": ?1}'))]
Can some one help me with this.?
Any help is really appreciated.
Today, I will show you way to implement Spring Data JPA Repository query in Spring Boot with Derived Query methods: Configure Spring Boot application to work with different database Typically, a Derived Query method has 2 elements: subject (the action), and predicate (the conditions).
Select Query In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm.xml file.
You can find the full list at query method subject keywords and query method predicate keywords. Let me explain it briefly. Tutorial data model class correspond to entity and table tutorials. TutorialRepository is an interface that extends JpaRepository for derived query methods. It will be autowired in SpringBootQueryExampleApplication.
Persist a JSON Object Using Hibernate 1 Overview. Some projects may require JSON objects to be persisted in a relational database. ... 2 Dependencies. Note that these techniques are not limited to these two libraries. ... 3 Serialize and Deserialize Methods. ... 4 Attribute Converter. ... 5 Conclusion. ...
You must use native query to use database functions like json_contains:
@Query("select c from CaseMessage c where c.caseId=?1 and c.id not in(select cm.id from CaseMessage cm where json_contains(status, '{\"status\": \"delete\"}') and json_contains(status, '{\"user_id\": ?2}'))", nativeQuery = true)
List<CaseMessageResponse> getAllCaseMessages(long caseId, long userId);
or with the @NativeQuery annotation
for more information :
Difference between query, native query, named query and typed query
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With