Spring Data Jpa Query dsl with fetch join - java

Is it possible to specify Join Fetch when using QueryDsl and Spring Data Repository?

No, there's no keyword in Spring Data JPA to trigger a fetch.
But you can write a Custom Repository and implement a query using Querydsl there.

Related

How does return SUM() in spring data jpa

I am uning Spring boot with spring data jpa.
I've made repository that extends JpaRepository and I want to get SUM() of table results.
table schema is like
point
user_index, user_point
and query to get point total is
select SUM(user_point)
from point
where user_index = 1;
what is the best way to get this result?
I am considering to use native query, but I think to use JPA Repository is better.
Have you tried the query you wrote? I believe jpa query does support sum operator
#Query(SELECT SUM(p.user_point) FROM point p WHERE p.user_index = :user_index)
float totalPointByUser(#Param("user_index") Long user_index)

JPA: No join method for Fetch interface

Via JPA Criteria Api
a Join can in turn be joined either via Join.join(...) or via Join.fetch(...).
but a Fetch in turn can only be joined via Fetch.fetch(...):
What is the reason? Why an attribute of a fetched entity in turn can not be joined (without fetch)?
For example:
root.join(entity1_attribute, JoinType.INNER)
.join(entity2_attribute, JoinType.INNER)
.fetch(entity3_attribute, JoinType.INNER)
is possible.
But
root.join(entity1_attribute, JoinType.INNER)
.fetch(entity2_attribute, JoinType.INNER)
.join(entity3_attribute, JoinType.INNER) //<--
is not possible.
It is this way for the same reason that the JPQL BNF does not allow aliasing fetch joins, because nested fetches do not have to be supported according to the JPA specification. You can ask on the JPA mailing list or create an issue to request making Fetch extend Join or something like that if you want.

Spring Data JPA #Modifying annotation usage with #Transactional

I am new in Spring Data JPA and will use #Modifying(clearAutomatically = true, flushAutomatically = true) annotation for an update method that updated a name field of Product entity. I have 2 questions that made me confused:
1. Do we need to use #Transactional with #Modifying annotation?
2. What does #Modifying exactly do? I read several pages e.g. Spring Data JPA #Modifying Annotation, and now too confused.
Second question first: the Modifying annotation lets you execute DML (inserts, updates, deletes) and DDL using JPA Query annotations where you put the DML or DDL where the query would normally go.
To answer the first question, i would expect to use this in a service layer annotated with #Transactional instead of putting the annotation on the Repository, because these operations seem likely to occur as part of a bigger operation with other business logic. But if that is not the case then the annotation could go on the Repository.

spring data JPA: How to execute aggregate functions

I am using Spring Data JPA and I have a requirement where I need to execute the SQL query as below:
select key_1, sum(key_2), sum(key_3) from table_1
where key_1 in ('1', '2') and {dynamic_columns} group by key_1;
For dynamic columns, I am using Spring Data JPA Specifications but I am not sure how can I write the aggregate functions like sum() on multiple columns.
With Spring Data JPA you have a few options in a spring repository.
References:
Baeldung - Spring data JPA
Spring.io - Spring data JPA
Specify a native query
#Query(value="select key_1, sum(key_2) ...", nativeQuery=true)
Optional<Object[]> getAggregates;
Use the providers (HQL for hibernate etc. query language)
#Query(value="select e.key_1, sum(e.key_2) from entity e ...")
Optional<Object[]> getAggregates;
You'll use objects here because you are not returning a specific entity you are adding custom (aggregate) columns. If you were returning a specific entity with a JPA repository you could return that entity instead of Object[]. Each item inside the object array will correspond to a columnof data, if you had multiple rows here you would use:
Optional<List<Object[]>> getAggregates;
Finally, if you have not used optionals before you will get your object array by:
if(objectsOptional.isPresent()) {
Objects[] objects = objectsOptional.get();
...
}
If this isn't what you were looking for i'll need more information to help you out.

How to perform some operation based on SQL return code I am using spring data JPA

I am working on some project where I used spring data jpa to query against DB2. Suppose used query "Select a,b from sometable where a='XX' ". So now based on sql code I want to perform some operation but how I will get sql return code as I am using spring data JPA.
Thanks in advance

Categories