How to Write Spring Data JPA query for getting two counts after group by - spring-boot

I have a table like this
I want to write something like this
SELECT oi.order_id,
COUNT(oi.found) as found,
(COUNT(1) - COUNT(oi.found)) as not_found
FROM orders_items oi
WHERE oi.order_id = 43
GROUP BY oi.order_id
Is it possible to write a query like this in Spring Data JPA?

Yes you can,
You need to have an interface annotated with #Repository and that extends the JpaRepository. There you can create queries, when the out of the box syntax does not cover your needs you can annotate a method with #Query and write your query likes this.
#Query("SELECT oi.order_id FROM order_items oi ...")
List<MyEntityClass> findMyEntities()
You can pass parameters to use also in the method. The syntax is not identical with the native one because jpa offers some abstraction. For small examples you can look here: https://www.baeldung.com/spring-data-jpa-query

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 ?

join more than one table in spring jparepository

I am trying to fetch record by doing a join. I am new to spring jparepository.
I understand that there is separate repository for each entity(table) where when i implement i need to define the entity and datatype of primary key.
Could anyone please suggest how can I fetch record by joining two tables.
I have two repo as below:
public interface AEntityRepository extends JpaRepository<AEntity, Integer>
public interface BEntityRepository extends JpaRepository<BEntity, Integer>
I want to join above two entity(AEntity, BEntity).
I know I can have custom query using something like below:
#Query("SELECT ****** FROM AEntity ae")
AEntity findCustomrRecords();
However can I write the same kind of query (join query) with join.
Do i need to have a separate repository implementing some other class.
Can anyone please help.
I am using mysql.
I understand that there is separate repository for each entity(table)
This is a very common misunderstanding. You do not want to have a repository per entity, but per aggregate root. See http://static.olivergierke.de/lectures/ddd-and-spring/
Regarding your specific problem at hand: Creating a custom method in your repository interface and annotating it with a JPQL should do the trick. So you get something like:
#Query("select a from BEntity b join b.a a where b.foo = :foo")
AEntity getAllFooishAs(String foo);
You can use any join syntax JPQL offers in the query.

Using referencing class field as filter in Spring Data Repository

Context: I have three models, Owner, Property and Community. Property has a reference to its Owner, and another one to the Community.
I need to make the following query: find all the owners in a community which meet some criteria (floor number, property letter, etc, all the fields of the criteria are inside the Property class)
Is there any way to implement this in a repository without creating a bidirectional relationship or writing a native query? Something like:
Set<Owners> findAllByCommunityAndProperty_floorNumberAndProperty_letter(Community community, Property property);
(I would need a bidirectional relationship to make the query above)
You can use a query like this
SELECT o
FROM Property p
INNER JOIN property.owners o
WHERE p. ...
See http://www.objectdb.com/java/jpa/query/jpql/from#INNER_JOIN_ for various examples of join syntax.
In Spring Data JPA you will probably use the #Query annotation to bind that query to a method.

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[].

Resources