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);
Related
How can I rewrite the following query using Spring Specifications.
#Query(value = "SELECT * FROM product WHERE attributes #> CAST(:jsonObject AS JSONB)", nativeQuery = true)
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
I have a spring jpa native query(actual query has multiple tables connected) something like below.
#Query(nativeQuery = true, value="
select id, name from TABLE where id NOT IN ('2', '3')")
List<Object> getValueForNOTIN()
#Query(nativeQuery = true, value="
select id, name from TABLE where id IN ('4', '5')")
List<Object> getValueForIN()
Instead of 2 methods, I want to use one method which replaces NOT IN and IN with this 'replaceClause' value.
List<Object> getValueForBoth(#Param("replaceClause)" String replaceClause)
I get error on start of server. Can't I do like this?
You can use a static string like
private String SELECT_NOT_IN_QUERY = "select id, name from TABLE
where id NOT IN ('id1', 'id2')"
private String SELECT_IN_QUERY = "select id, name from TABLE
where id IN ('id1', 'id2')"
then look for id1 and id2 and replace with its values. use the right static string accordingly.
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);
Given this two entities:
post post_category
- id - post_id
- title - name
- text
I'd like to make this query using jpa criteria query:
select * from post
where post.id in (
select post_id from post_category
where name = '<category1>' and name = '<category2>' ... and name = '<categoryN>')
Looking at your query sketch I think it will not return any results. I have changed it to mean name in ('<category1>','<category2>', ...) instead. Which may not match your case.
This assumes that you have your mapping set up properly on your classes for JPA to work. For example
class PostCategory {
private String name;
private Post post;
...
#ManyToOne
public Post getPost() {
return post;
}
...
In which case your DAO code would resemble this (given a List nameValues object)
// Set up the subquery
DetachedCriteria dc = DetachedCriteria.forClass(PostCategory.class)
.setProjection(Projections.property("post.id"))
.add(Restrictions.in("name", nameValues));
Criteria crit = session.createCriteria(Post.class)
.add(Subqueries.eqProperty("id", dc));
But if you want a list of Post objects which have ALL of the categories then you will need to make a separate DetachedCriteria instance for each category and add each as an AND'ed Subquery.