推断类型变量

时间:2018-08-28 20:16:47

标签: java spring-mvc web thymeleaf

我需要一些帮助,我不知道该怎么办 我遇到了这些错误,我无法解决它们,我已经尝试了几件事,但到目前为止没有任何效果

错误1:

  

错误:(56,35)java:接口org.springframework.data.repository.query.QueryByExampleExecutor中存在方法无法应用于给定类型;     必需:org.springframework.data.domain.Example     找到:java.lang.Integer     原因:无法推断类型变量S       (参数不匹配; java.lang.Integer无法转换为org.springframework.data.domain.Example)

错误2:

  

错误:(60,49)java:接口org.springframework.data.repository.query.QueryByExampleExecutor中的findOne方法不能应用于给定类型;     必需:org.springframework.data.domain.Example     找到:java.lang.Integer     原因:无法推断类型变量S       (参数不匹配; java.lang.Integer无法转换为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";
    }
}

这是我的文章实体

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

}

1 个答案:

答案 0 :(得分:0)

请参阅类似问题的答案:https://stackoverflow.com/a/49224641/2031954

TL,DR::方法existsfindOne接收到一个Example<S>的实例作为参数,因此您使用它们的方式不正确。改用existsByIdfindById

相关问题