简体   繁体   中英

Spring Boot custom query group by and count

I'm having a problem with accessing data from a custom query.

Here is the POJO:

@Getter
@Setter
@NoArgsContructor
@AllArgsConstructor
@Entity
@Table(name = "run_list")    
public class RunList {
    @Id
    @Column(name = "id")
    private Long id;

    @Column(name = "control_run_name"
    private String serverName;

    @Column(name = "control_run_date"
    private Date controlRunDate

<.. bunch of other fields .. >

Here is the repository:

public interface RunListRepository extends JpaRepository<RunList, Long> {

    @Query("SELECT u.serverName,count(u) as controlRunCount from RunList u where u.controlRunDate < :lastUploadDate group by u.serverName")
    List<RunList> findAllControlRunAfterDate(@Param("lastUploadDate") Date lastUploadDate);

In the controller I invoke the repository method like so:

Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2020-03-01");
model.addAttribute("runList",runListRepository.findAllControlRunAfterDate(date);

The binding to the date in the query works ok I get a first result of the group by so that is solved. The thing is I get an error while runing this:

Failed to convert from type[java.lang.Object[]] to type [@org.springframework.data.jpa.Query my.package.name.RunList} for value '{server1,14}';

When I use the SQL query against the database from it's CLI I get a nice grouping of the serverName and the count. I'm guessing the problem is in the conversion of the count field that is popping out of nowhere from the repository method and Spring doesn't know what to link this too.

I've tried to use a RunListDTO here that has only a String serverName and an Ingeter controlRunCount with no luck - the repository interface didn't like me using a DTO in the output of a method used in a interface created with.

Is there a way to make this a custom Array/Object in flight when the repository does the count?

Regards, Jarek.

Since you are selecting a field and a count there is no way it can be mapped to your entity, so this query returns an array of values you specified, like you can see in the exception: ' {server1,14}' .

public interface RunListRepository extends JpaRepository<RunList, Long> {

    @Query("SELECT u.serverName,count(u) as controlRunCount from RunList u where u.controlRunDate < :lastUploadDate group by u.serverName")
    List<Object[]> findAllControlRunAfterDate(@Param("lastUploadDate") Date lastUploadDate);

So in your service you can work with this data like:

List<Object[]> list = runListRepository.findAllControlRunAfterDate(yourDate);
for (Object[] ob : list){
    String serverName = (String)ob[0];
    Integer count = (Integer)ob[1];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM