union all in jpa query code example

Example 1: spring jpa query with union all

@Query(nativeQuery = true,value = "SELECT re.id,re.i_amount,re.i_share,re.create_time,re.type FROM ( " +        "SELECT id AS id, i_amount AS i_amount, i_share AS i_share, create_time AS create_time , 1 AS type " +        "FROM f_project where project_id = :projectId " +        "UNION ALL " +        "SELECT id AS id, i_amount AS i_amount, i_share AS i_share, create_time AS create_time ,2 AS type " +        "FROM l_project  where project_id = :projectId  ) re"        ,countQuery = "SELECT count(re.id) FROM ( SELECT id  FROM f_project  where project_id = :projectId UNION ALL SELECT id  FROM l_project  where project_id = :projectId  )  as re")Page<Map<String,Object>> find(@Param("projectId") long projectId, Pageable pageable);

Example 2: spring jpa query with union all

Pageable pageable = PageRequest.of(pageQueryDto.getPage() - 1, pageQueryDto.getSize(), Sort.Direction.fromString(pageQueryDto.getDirection()), propertie);Page<Map<String, Object>> page = projectRepository.find(projectId, pageable);

Tags:

Java Example