Crudrepository findBy函数仅在未找到任何内容时起作用

时间:2019-04-23 10:16:55

标签: java spring jpa

我第一次在存储库中实现findByContentContaining函数时,everinging一切正常。但是现在,当我调用该函数并且如果找到了东西时,它将给我一个错误。如果找不到任何内容,则返回一个空列表。

这是我的存储库(除findByContentContaining函数外,其他所有功能都可以使用):

public interface KweetRepo extends CrudRepository<Kweet, Integer> {

    Kweet getById(Integer id);

    List<Kweet> findTop10ByPosterId(int id);

    List<Kweet> findByContentContaining(String content);

    List<Kweet> findByPosterId(int id);

}

这是我的Kweet课程的模型

@Entity
public class Kweet implements Comparable<Kweet>{

    @Id
    @GeneratedValue
    private int id;

    @Column(name="content")
    private String content;

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Date dateTime;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id")
    private User poster;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name="KweetLikedBy")
    private List<User> likedBy;

    public Kweet() {
    }

    public Kweet(String content, Date dateTime, User poster, List<User> likedBy) {
        this.content = content;
        this.dateTime = dateTime;
        this.poster = poster;
        this.likedBy = likedBy;
    }

    public int getId() {
        return id;
    }

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

    public String getContent() {
        return content;
    }

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

    public Date getDateTime() {
        return dateTime;
    }

    public void setDateTime(Date dateTime) {
        this.dateTime = dateTime;
    }

    public int getPoster() {
        return poster.getId();
    }

    public void setPoster(User poster) {
        this.poster = poster;
    }

    public List<User> getLikedBy() {
        return likedBy;
    }

    public void addLikedBy(User user) {
        this.likedBy.add(user);
    }

    public void removeLikedBy(User user) {
        this.likedBy.remove(user);
    }

    @Override
    public int compareTo(Kweet o) {
        return getDateTime().compareTo(o.getDateTime());
    }
}

0 个答案:

没有答案