mardi 28 août 2018

Inferred type-variable(S)

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> {

}




Aucun commentaire:

Enregistrer un commentaire