Correction of route /api/posts/last from lazy relations.

This commit is contained in:
2019-01-23 22:19:06 +01:00
parent cff738b91a
commit 55940c5fe7
2 changed files with 6 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import java.util.List;
import java.util.Optional;
import org.codiki.core.entities.persistence.Post;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
@@ -15,9 +16,9 @@ public interface PostRepository extends CrudRepository<Post, Long>, PostSearchRe
@Query("SELECT p FROM Post p WHERE p.key = :postKey")
Optional<Post> getByKey(@Param("postKey") final String pPostKey);
@Query(value = "SELECT * FROM post p INNER JOIN \"user\" u ON u.id = p.creator_id ORDER BY p.creation_date DESC LIMIT :limit",
nativeQuery = true)
List<Post> getLast(@Param("limit") final Integer pLimit);
// TODO : Remove "JOIN FETCH u.role", actually necessary for JSON serialization
@Query("SELECT p FROM Post p JOIN FETCH p.author u JOIN FETCH u.role ORDER BY p.creationDate DESC")
List<Post> getLast(Pageable pPageable);
@Query(value = "SELECT * FROM post p INNER JOIN \"user\" u ON u.id = creator_id WHERE category_id = :categoryId ORDER BY creation_date DESC",
nativeQuery = true)

View File

@@ -18,6 +18,7 @@ import org.codiki.core.repositories.PostRepository;
import org.codiki.core.services.ParserService;
import org.codiki.core.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -78,7 +79,7 @@ public class PostController {
@GetMapping("/last")
public List<PostDTO> getLast() {
return postRepository.getLast(LIMIT_POSTS_HOME).stream()
return postRepository.getLast(PageRequest.of(0, LIMIT_POSTS_HOME)).stream()
.map(PostDTO::new).collect(Collectors.toList());
}