Spring data jpa select specific columns 

user_id =:userId", nativeQuery=true) List getUserFullNameById(@Param("userId") String userId); May 12, 2018 · Spring Data will select only required columns. secondProperty FROM ObjectName i WHERE i. id=10. getNameAndCityOnly(); return empList; } ---- In Repo ---- public interface EmployeeRepository extends CrudRepository { @Query("select e. job AS job FROM CustomerEntity customer") List findAllJobs(); Conclusion Nov 03, 2018 · In this short Spring Data JPA tutorial, you will learn how to create a JPA Native SQL Query to select specific columns only. secondProperty) FROM ObjectName i WHERE i. JPA Native SQL Query to Select Specific Columns. name, e. Below is an example of JPA Native Query that selects from a database table called Users only two columns: first_name and, last_name @Query(value="select first_name, last_name from Users u where u. @Query("SELECT fullName AS fullName, customer. Make sure to use aliases to the fields, as without those it won’t work. class);List. Feb 25, 2014 · Using Spring Data JPA there is a provision to select specific columns from database---- In DAOImpl ----@Override @Transactional public List getAllEmployee() throws Exception { LOGGER. Another way is to wrap the selected properties in a custom object and execute it in a TypedQuery: String query = "SELECT NEW CustomObject(i. Executing this query will return a list of Object[], where each array contains the selected properties of one object. firstProperty, i. It works with both generated queries from method name and @Query annotation. city from Employee e" ) List getNameAndCityOnly(); } It worked Nov 08, 2021 · Spring handles this “on the fly”. getNameAndCityOnly(); return empList; } ---- In Repo ---- Dec 21, 2019 · Using Spring Data JPA there is a provision to select specific columns from database ---- In DAOImpl ---- @Override @Transactional public List getAllEmployee() throws Exception { LOGGER. Then in the @Query annotation of this method, the JPQL query is specified. id=10";TypedQuery typedQuery = em. Related question: Spring Data JPA Projection selected fields from the DB Jul 12, 2014 · SELECT i. info("Inside getAllEmployee"); List empList = empRepo. createQuery(query , CustomObject