0

i need some help with this i don't get what to do i got those errors and i cant handle with them i've tried few things but nothing worked so far

Error 1:

Error:(56, 35) java: method exists in interface org.springframework.data.repository.query.QueryByExampleExecutor cannot be applied to given types; required: org.springframework.data.domain.Example found: java.lang.Integer reason: cannot infer type-variable(s) S (argument mismatch; java.lang.Integer cannot be converted to org.springframework.data.domain.Example)

Error 2:

Error:(60, 49) java: method findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor cannot be applied to given types; required: org.springframework.data.domain.Example found: java.lang.Integer reason: cannot infer type-variable(s) S (argument mismatch; java.lang.Integer cannot be converted to org.springframework.data.domain.Example)

ArticleController.java

@Controller
public class ArticleController {
    @Autowired
    private ArticleRepository articleRepository;
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/article/create")
    @PreAuthorize("isAuthenticated()")
    public String create(Model model){
        model.addAttribute("view", "article/create");

        return "base-layout";
    }

    //method calling for creating the article
    @PostMapping("/article/create")
    @PreAuthorize("isAuthenticated()")
    public String createProcess(ArticleBindingModel articleBindingModel){
        UserDetails user = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();

        User userEntity = 
        this.userRepository.findByEmail(user.getUsername());

        Article articleEntity = new Article(
                articleBindingModel.getTitle(),
                articleBindingModel.getContent(),
                userEntity
        );

        this.articleRepository.saveAndFlush(articleEntity);

        return "redirect:/";
    }

    @GetMapping("/article/{id}")
    public String details(Model model, @PathVariable Integer id){

        if(!this.articleRepository.exists(id)){
            return "redirect:/";
        }

        Article article = this.articleRepository.findOne(id);

        model.addAttribute("article", article);
        model.addAttribute("view", "article/details");

        return "base-layout";
    }
}

Here is my Article Entity

@Entity
@Table(name = "articles")
public class Article {
    private Integer id;
    private String title;
    private String content;
    private User author;


    public Article() {

    }
    //article constructor
    public Article(String title, String content, User author){
        this.title = title;
        this.content = content;
        this.author = author;
    }

    @Transient
    public String getSummary(){
        return this.getContent().substring(0, this.getContent().length() /2) + ". . .";
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(nullable = false)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column(columnDefinition = "text", nullable = false)
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @ManyToOne()
    @JoinColumn(nullable = false, name = "authorId")
    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

}

ArticleRepository.java

public interface ArticleRepository extends JpaRepository<Article, Integer> {

}
Rumen
  • 57
  • 1
  • 11

1 Answers1

1

See the answer to a similar question: https://stackoverflow.com/a/49224641/2031954

TL,DR: the methods exists and findOne receive an instance of Example<S> as the argument, so you use them incorrectly. Use existsById and findById instead.

Enfernuz
  • 51
  • 4
  • 2
    It fixed the first error with exist but when i change findOne(id) to findById(id) it says >Error:(61, 58) java: incompatible types: java.util.Optional cannot be converted to test.blog.blog.Entity.Article i got it fixed with Optional
    article = this.articleRepository.findById(id);
    – Rumen Aug 28 '18 at 22:21
  • @Rumen yes, `findById(id)` will return an `Optional
    `. You can retrieve the value with `.get()`: `Optional
    result = articleRepository.findById(id); if(result.isPresent()) { Article article = result.get(); ..... }` Or even simpler: `Article article = articleRepository.findById(id).orElse(null); if(article != null) { ..... }`
    – Antoine Dahan Jan 19 '21 at 18:23