Spring data querying with complex object marked by #Param - spring

I have an entity with aggregation information that I am going to receive from database:
class BookStats {
String author
String title
Integer count
}
My question is could I use some complex object in Repository to filter statistic information. Something like that:
#Query(value = "SELECT new com.test.book.BookStats(b.author, b.title, count(b)) from Book b where b.title = :filter.title and b.author= :filter.author")
List<BookStats> calculateBookStats (#Param("filter") Filter filter)

Spring Data JPA allows to use SpEL :
#Query(value = "SELECT new com.test.book.BookStats(b.author, b.title, count(b)) from Book b where b.title = :#{#filter.title} and b.author= :#{#filter.author}")
List<BookStats> calculateBookStats (#Param("filter") Filter filter)
More info here.

Related

How to get Distinct record from JPA

I have implemented a method which gives me specification, but what I want is the query should be as below:
Select Distinct *
From (another select query)
I generate query dynamically.
How do I perform the same using specification in Spring Boot?
Try something like this
Specification<T> spec = getSpecification();
Specification<T> distinctSpec = (root, query, cb) -> {
query.distinct(true);
return spec.toPredicate(root, query, cb);
};
if you want to get distinct records, you have to write a query like this in the repository.
The below query gives the distinct author from the post table.
#Query("select distinct author from Post")
List<String> findUniqueAuthor();
Write this in the repository
#Query(value = "Select Distinct * From (another select query)", nativeQuery = true)
List<Object> findUniqueData();

spring data jpa with native query

I am trying spring data JPA. My repository method is like this:
#Query(value = "SELECT * FROM test_result WHERE FK_TEST_PAPER_ID=?1 AND PERCENTAGE_OBTAINED BETWEEN :?2 AND :?3 ?#{#pageable}", nativeQuery = true)
Page<TestResult> findResult(long testPaperId, Float minpercentage, Float maxpercentage, Pageable pageInfo);
My service implementation code is like this:
Page<TestResult> getAllResult1 = testResultRepository.findResult(testPaperId,minpercentage,maxpercentage,pageInfo);
And I am getting this error : Paging query needs to have a Pageable parameter!
I am new to spring. Please help.
Remove ?#{#pageable} from query

how to select value and count in spring jpa?

i have table named gifts that contains field company_value_id and i want to make select for all company_value_id,count(company_value_id) so that the result will be list of object and each object will contain company_value_id,count(company_value_id)
i am using spring jpa with annotations as follows:
public interface GiftsRepository extends JpaRepository<Gifts, String> {
#Query("from Gifts g where g.companyGuid = :companyGuid")
List<Gifts> getGiftsByCompany(#Param("companyGuid") String companyGuid);
}
please advise, thanks.
i was able to accomplish it as follows:
#Query("select g.value.id,cr.value.name,count(g.value.id) from Gift g where g.user.id=:userId group by g.value")
List<Object[]> getUserGifts(
#Param("userId") String userId);
and in the service layer i extract the values as follows:
List<Object[]> results = giftsRepository
.getUserGifts(userId);
for (Object[] result : results) {
String id = (String) result[0];
String name = (String) result[1];
int count = ((Number) result[2]).intValue();
}
You need add a parameter to your function,just like this:
#Query("from Gifts g where g.companyGuid = :companyGuid")
List<Gifts> getGiftsByCompany(#Param("companyGuid") String companyGuid,Pageable pageable);
and the pageabel can be create like this:
Pageable pageable = new PageRequest(pageIndex, pageSize, Direction.ASC, sortColumn);

Using distinct in Spring data over multiple columns

My domain model is like this:
CollectedData {
String name;
String description;
int count;
int xAxis,
int yAxis
}
Using Spring data repository query, I would like to retrieve all the unique rows (unique with name, xAxis, yAxis)
I am trying something like this
#Query("select distinct a.name, a.xAxis, a.yAxis from CollectedData a")
List<CollectedData> findAllDistinctData();
So, when I do
List<CollectedData> records= findAllDistinctData();
for (CollectedData record : records) { //Exception on this line
}
Exception
[Ljava.lang.Object; cannot be cast to CollectedData.
Is there any other way to write query for this ?
#Query return ArrayList of Object(s) instead of specific type of object. so you have to define some thing like
#Query("select distinct a.name, a.xAxis, a.yAxis from CollectedData a")
List<Object> findAllDistinctData();
then cast according to your requirement,
List<Object> cdataList=findAllDistinctData();
for (Object cdata:cdataList) {
Object[] obj= (Object[]) cdata;
String name = (String)obj[0];
String description = (String)obj[1];;
...
}
Instead of returning an object you can use JPA's constructor expression feature to return a more specific object holding only the columns you're interested in. See also following answer:
JPQL Constructor Expression - org.hibernate.hql.ast.QuerySyntaxException:Table is not mapped
According to your example you could create a new Object with only the columns you are interested in:
SELECT DISTINCT new com.mypackage.MyInterestingCollectedData(a.name, a.xAxis, a.yAxis) from CollectedData a
If you want to select complete object based on distinct values of multiple columns,
In that case the native query would be the option.
e.g.
#Query(
value = "select distinct on (column1, column2, column3) * From my_table where someId=: order by column1 asc,column2 desc,column3 desc,column4 desc",
nativeQuery = true
)
fun finalAllDistinctBy(containerId: String): List<MyTable>

How can I initialize lazy associations using Spring Data JPA when the query is specified in #Query annotation?

Here's my query before I converted over to Spring Data JPA. Notice how I used to use Hibernate.initialize() to manually fetch the widget's messages.
public Object findWidget(final Widget findMe) {
Widget widget = getJpaTemplate().execute(new JpaCallback<Widget>() {
public Widget doInJpa(EntityManager em) throws PersistenceException {
Query q = em.createQuery("SELECT h FROM " + entityClass.getName() + " h where h.widgetId = ? ");
q.setParameter(1, findMe.getId());
Widget found = (Widget)q.getSingleResult();
//Initialize lazy associations
if(found!= null){
Hibernate.initialize(widget.getMessages());
}
return found;
}
});
return widget;
}
And here's what my query function looks like now. Notice there is no body to put the Hibernate.initialize() in.
#Query("SELECT h FROM Widget h where h.widgetId = ?1 ")
public AccessPoint findWidget(String widgetId);
So how can I specify that the widget's messages are to be fetched actively and not lazily?
Try a fetch join, something like this:
#Query("SELECT h FROM Widget h LEFT JOIN FETCH h.messages WHERE h.widgetId = ?1 ")
public AccessPoint findWidget(String widgetId);
http://docs.oracle.com/html/E24396_01/ejb3_langref.html#ejb3_langref_fetch_joins
Soon after I posted this question, I realized that I am trying to put functionality into the DAO layer that really belongs in the service layer.
So now I initialize the lazy associations using Hibernate.initialize(widget.getMessages()) in my WidgetService class, after I call WidgetDAO.findWidget().
I really should have been doing it this way all along.
EDIT: #MikeN has a good point. A fetch join is the real answer since it is implementation-independent and gets all necessary information in the original query.

Resources