Create @Query select without Entity table class

57 views Asked by At

how should be looks like code which return Json from database without @Table(tableName) @Entity etc. ? for example , that CustomResponse is not mapped to database table, but by that select I want get same result as normal @Entity @Table class

    {   
      name : "myname",
      count: 5 
     }

@Query(value = """
            SELECT name,count from mytable            
            """
            ,nativeQuery = true)
    List<CustomResponse> getData();

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CustomResponse {
    private String name;
    private Integer count;
}
1

There are 1 answers

3
Oscar On

Try this using JPA constructor expresion :

@Query(value = "SELECT new <add here CustomResponse class package name>.CustomResponse(m.name, m.count) FROM MyEntity AS m")
List<CustomResponse> getData();