How can I rewrite the a query containing a filter on a JSONB column using spring specifications - spring

How can I rewrite the following query using Spring Specifications.
#Query(value = "SELECT * FROM product WHERE attributes #> CAST(:jsonObject AS JSONB)", nativeQuery = true)

Related

How to query all posts by tag_id in Postgres ManyToMany Relationship in spring JPA repository?

How to query all posts by tag_id in Postgres ManyToMany relationship in Spring JPA repository?
These are the tables:
post
post_tag
tag
id
id_post
id
..
id_tag
..
If I query the PostgreSQL directly via pg_admin Query tool the following sql is working:
SELECT * FROM post WHERE ID IN (
select post_id from post_tag tag WHERE (tag_id = '12'))
I like to have this query in my JPA Repository, but the following isn't working.
#Query(value = "SELECT * FROM post WHERE ID IN (select post_id from
post_tag tag WHERE (tag_id = id))",nativeQuery = true,)
fun findPostsByTagId(id: String): Optional<List<Post>>
How to make this work?
Thanks in advance.
This did the trick:
#Query(value = "SELECT * FROM post WHERE ID IN (select post_id from
post_tag tag WHERE (tag_id = :id))",nativeQuery = true,)
fun findPostsByTagId(id: Long): Optional<List<Post>>
add ":" in front of "id" like sidgate says
changing String to Long for the passed parameter

Problem with MongoDB #Query annotation/Query

I got problems with #Query annotation or Query query = new Query() - I can't include/exclude some fields.
I find code samples like:
#Query(value = "{'id': ?0}",fields = "{'id':1}")
User findUserById(String id);
Result should be user with only one field -> id but its showing other fields to..
So I found other samples like:
Query query = new Query();
query.fields().include("id");
query.addCriteria(Criteria.where("id").is(id));
User one = mongoTemplate.findOne(query, User.class);
Same result there..
Any ideas?
Integer findUserById(String id);
Use the DataType of Id [like Integer] to get only id.
Change datatype of according the requirement/usecase

can we pass parameters in view of a table in SQL?

Is there any way to pass parameters to a view in postgresql? or any alternative of passing parameter from spring jpa native query
You have 2 ways to pass params to a query in JPA
Using index, here spring data will pass method parameters to the query in the same order they appear in the method declaration:
#Query(value = "SELECT * FROM FOO WHERE id = ?1", nativeQuery = true)
findById(Integer id);
By Names using #Param annotation in the method declaration to match parameters defined
#Query(value = "SELECT * FROM FOO WHERE id = :id", nativeQuery = true)
findById(#Param("id")Integer id);

How to use Customize your Queries in JPA with Query Methods (spring boot)

I want to retrieve a specific value from the database based on a criteria without using the query method in Spring JPA.
they query desired is
SELECT TOP 1 * FROM Co2 WHERE Co2.room = ?1 order by co2.id desc
which can be used in a normal native query annotation like so:
public interface Co2Respository extends CrudRepository<Co2, Integer> {
#Query("SELECT TOP 1 * FROM Co2 WHERE Co2.room = ?1 order by co2.id desc",
nativeQuery = true)
Co2 findLastInsertedValueForRoom(int id);
}
the question is how to achieve the same using the custom query method in Spring JPA
I will answer my own question,
the equivalent custom method for the query mentioned above is:
Co2 findTopByRoomOrderByIdDesc(Room room);

How to make a custom sorting query in spring boot for a mongo db repository?

I want to put this query with #Query annotation in my repository.
This is the query:
`db.report.find({'company' : 'Random'}).sort( { 'reportDate' : -1} ).limit(1)`
Which is the best way to implement custom queries with #Query annotations or to use MongoTemplate ?
Using Mongo Template.
Criteria find = Criteria.where("company").is("Random");
Query query = new Query().addCriteria(find).with(new Sort(Sort.Direction.DESC, "reportDate"));
BasicDBObject result = mongoOperations.findOne(query, BasicDBObject.class, "collection_name");
Using Mongo Repository
Report findTopByCompanyOrderByReportDateDesc(String company)
Note that in the new version of Springboot(v2.2.5), the correct method is sort.by().
like this:
Query query = new Query().addCriteria(find).with(Sort.by(Sort.Direction.DESC, "reportDate"));

Resources