I want to add in my Repository interface a method for the following SQL query:
SELECT ID, NAME FROM TABLE_NAME
This SQL query works as expected, but I want to write it as a JPA query method, I've tried in many ways but didn't get it working, Please help me.
Following which I've tried but didn't work:
findAllByIdName(){}
findAllByIdAndName(){}
findByIdName(){}
findByIdAndName(){}
Create a result class first:
package com.example;
public class ResultClass{
private Long id;
private String name;
public ResultCalss(Long id, String name){
// set
}
}
and then use a custom @Query:
@Query("select new com.example.ResultClass(e.id, e.name) from MyEntity e")
public List<ResultClass> findIdsAndNames();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With