Alice
Alice

Reputation: 331

pageRequest cannot be cast to pageable with root

I am a Java Spring beginner. I am creating a blog project in which I need to load 5 latest posts. I got this problem and I don't know how to fix it //ignore me //ignore me //ignore me //ignore me //ignore me //ignore me //ignore me PostRepository:

import java.awt.print.Pageable;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface PostRepository extends CrudRepository<Post, Long>{
    @Query("SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.date DESC")
    List<Post> findLatest5Posts(Pageable pageable);
}

PostServicesImp:

import java.awt.print.Pageable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import havan.blog.demo.models.Post;
import havan.blog.demo.models.PostRepository;

@Service
@Primary
public class PostServicesImpl implements PostServices{
    @Autowired
    private PostRepository postRepo;

    @Override
    public List<Post> findAll() {
        // TODO Auto-generated method stub
        return (List<Post>) this.postRepo.findAll();
    }

    @Override
    public List<Post> findLatest5() {
        // TODO Auto-generated method stub
        return (List<Post>) this.postRepo.findLatest5Posts((Pageable)new PageRequest(0, 5));
    }

HomeController:

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import havan.blog.demo.models.LoginForm;
import havan.blog.demo.models.Post;
import havan.blog.demo.services.NotiService;
import havan.blog.demo.services.PostServices;
import havan.blog.demo.services.UserService;

@Controller
public class HomeController {

    @Autowired
    private PostServices pServices;

    @RequestMapping(path="/*")
    public String index(Model model){
        List<Post> latest5Posts=pServices.findLatest5();
        model.addAttribute("latest5posts",latest5Posts);

        List<Post> allPosts= pServices.findAll();
        model.addAttribute("allPosts",allPosts);

        return "index";
    }
}

//ignore me //ignore me //ignore me //ignore me //ignore me //ignore me //ignore me

Upvotes: 2

Views: 9047

Answers (2)

ol_fsd
ol_fsd

Reputation: 1

Import Pageable Class from org.springframework.data.domain.Pageable instead of java.awt.print.Pageable

Upvotes: -1

Martin Ondo-Eštok
Martin Ondo-Eštok

Reputation: 1043

It's late but maybe it could help to someone else.

You have imported class Pageable from java.awt.print.Pageable but you need Pageable from Spring's package: org.springframework.data.domain.Pageable

Upvotes: 32

Related Questions