Custom Object Return in Spring Data JPA GROUP BY Queries
Introduction
Retrieving data as custom objects from Spring Data JPA GROUP BY queries enhances data presentation and simplifies further processing. This guide explores how to achieve this, showcasing solutions for both JPQL and native queries.
JPQL Queries
JPQL queries within the JPA specification offer native support for returning custom objects.
Step 1: Create a Custom Bean
Define a simple bean class to represent the desired output structure:
<code class="java">public class SurveyAnswerStatistics { private String answer; private Long cnt; // Constructor }</code>
Step 2: Return Bean Instances
Update the repository method to return instances of the custom bean:
<code class="java">public interface SurveyRepository extends CrudRepository<survey long> { @Query("SELECT new com.path.to.SurveyAnswerStatistics(v.answer, COUNT(v)) FROM Survey v GROUP BY v.answer") List<surveyanswerstatistics> findSurveyCount(); }</surveyanswerstatistics></survey></code>
Native Queries
While native queries lack direct support for the new keyword, Spring Data Projection interfaces provide an alternative solution:
Step 1: Define a Projection Interface
Create a projection interface with properties corresponding to the desired output:
<code class="java">public interface SurveyAnswerStatistics { String getAnswer(); int getCnt(); }</code>
Step 2: Return Projected Properties
Update the repository method to return projected properties:
<code class="java">public interface SurveyRepository extends CrudRepository<survey long> { @Query(nativeQuery = true, value = "SELECT v.answer AS answer, COUNT(v) AS cnt FROM Survey v GROUP BY v.answer") List<surveyanswerstatistics> findSurveyCount(); }</surveyanswerstatistics></survey></code>
Employ the SQL AS keyword to map result fields to projection properties seamlessly.
The above is the detailed content of How to Return Custom Objects from Spring Data JPA GROUP BY Queries?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor