Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all the data of two columns from a table using JPA query method

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(){}
like image 555
tsoni Avatar asked Oct 24 '25 13:10

tsoni


1 Answers

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();
like image 102
Maciej Kowalski Avatar answered Oct 27 '25 03:10

Maciej Kowalski