spring boot 2.0
hibernate 5.2
위 상황에서 repository에 native query를 사용하고 싶어서 찾다봄
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import model.CustomClass;
public interface CustomClassRepository extends PagingAndSortingRepository<CustomClass, byte[]> {
// HQL
@Query(value = "select p" +
"from table_name as a" +
"where a.group.name like %:groupname%"
, countQuery = "select count(*) " +
"from table_name as a" +
"where a.group.name like %:groupname%"
)
Page<CustomClass> findByGroupNameLike(@Param("groupname") String groupname, Pageable pageable);
// native
// native 쿼리 사용시 pageable 사용방법
@Query(value = "select * " +
"from table_name as a " +
"where id = :id" +
" limit ?#{ #pageable.offset }, ?#{#pageable.offset + #pageable.pageSize}"
, countQuery = "select count(*) " +
"from table_name" +
"where id = :id"
, nativeQuery = true)
Page<Object[]> findAllMap(@Param("id") byte[] id, Pageable pageable);
}