How to write Subquery along with EXIST in Spring Boot JPA? - spring-boot

I would like to get a result as boolean using EXISTS keyword along with Subqueries. How to use subqueries in Spring boot JPA. Below is my query which works fine with sql, But not sure why not working with JPA Spring Boot.
EXAMPLE :JPA QRY: SELECT EXISTS(SELECT t.test from Test t where t.id = ?1)
Here I am getting true/false as expected with SQL, but getting below error with Spring boot JPA ?
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree
Could you please let me know how to write the query using JPA without native using Native Query.
Thanks

The JpaRepository exposes existsById method which does what you want
boolean exists = repository.existsById(searchId)
https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html#existsById(ID)

Related

Native Query in spring boot JPA

I'm using spring boot 2.6 and spring boot JPA version 2.6.4. I was interested in pull last five records from mysql table by using next query
#Query(value="SELECT * FROM produit p ORDER BY p.id DESC LIMIT 5")
public Collection getProduit();
Normally in the #Query block, i need to add the nativeQuery = true after the value property but the nativeQuery attribute is not defined. What am i missing?
Please help, already 5 hours searching on this.
When using a native query you need to define a mapping of the values to for example your own type. You could use interface based projections when you only need to read data see interface based projections. Then your could would look like
#Query(nativeQuery=true, value="SELECT * FROM produit p ORDER BY p.id DESC LIMIT 5")
Set<Projection> getProduit();
Another thing to check would be what import is used for the #Query annotation, maybe it is the wrong one.

How to use stream as where clause with Spring data jpa?

I'm using org.springframework.boot:spring-boot-starter-data-jpa:1.5.4.RELEASE in my springboot application. I tried few scenarios where I'd build a query using the query builder feature. eg -
#Query(value="select new Foo(b.name) from Bar b where b.id IN (:ids)")
Stream<Foo> findByIdIn(#Param("ids")List<String> ids);
Is it possible to use streams in the where clause? something like eg - Stream<Foo> findByIdIn(Stream<String> ids). If its possible then how should I change :ids in my #Query?
Can someone point me to an example ?

Springboot JPA #Query Rest Giving PersistentEntity must not be null

I am trying to put together a simple rest service with springboot 1.4.3. I have it up and running with simple queries like findByRecid, however when I try to do a #Query statement on that same entity, I get the below error message
{"cause":null,"message":"PersistentEntity must not be null!"}
Further, if I use a fully qualified name for the entity in the query, Intellij tells me that the class isn't an entity, even though it's marked with #Entity and works with the standard springboot queries. Please assist if possible - I've been trying to figure this one out for days. Below is the query for your reference
#Query("SELECT new com.test.domain.ReceivableBeans.RecAgeBucketGroupAmountSum(r.agebucket, sum(r.amount)) from com.test.domain.Receivable as r GROUP BY r.agebucket")
List<com.test.domain.ReceivableBeans.RecAgeBucketGroupAmountSum> recByAgeBucket();
Try:
#Query("select new ReceivableStats(r.agebucket, sum(r.amount)) from Receivable r group by r.agebucket")
List< ReceivableStats> recByAgeBucket();
or name your aliases in the query, e.g.
sum(r.amount) as amount
and have your method return an Object[].

How to limit the results of a Spring Repository with custom query

I am using a Spring repository to query a database, but because of the nature of my application I need to use a custom #Query in order to JOIN FETCH lazy collections.
This process works fine, but now I need to limit the result to a single record. I understand that Spring has the notion of findFirst or findTop1 in the method names, but this does not appear to work when you have a custom query.
How can I use a custom query and limit the result to 1 record when using a Spring repository?
you need to pass a Pageable param in your query method
#Query("select e from Entity e LEFT JOIN FETCH e.list")
public Page<Entity> find(Pageable pageable);
and call the method passing the object
repository.find(new PageRequest(0, 1));

Spring ROO sorting

I have a set of Entities generated using Spring ROO. After several commands, basically using web mvc, I have a set of links to get the contents from a database. I need to order the contents according to title, instead of the order in the table. How can I do it? Is there any Spring ROO commando or annotation to order the items.
For more information, I am using Spring Roo 1.2.1, and the finder I got is the following.
public static List<Deporte> Deporte.findAllDeportes() {
return entityManager().createQuery("SELECT o FROM Deporte o", Deporte.class).getResultList();
}
Thanks in advance.
Spring Roo uses JPA for the persistence. Search for JPA tutorials and documentation to find what you're looking for.
JPQL uses the order by clause to order results returned by a query. This order by is very similar to the SQL order by clause:
SELECT o FROM Deporte o order by o.title

Resources