【发布时间】:2019-07-01 19:25:32
【问题描述】:
我实现了一个存储库、一个实体和一个 POJO,我想从存储库调用 findAll() 方法,但它返回空指针异常,即使我已经填充了数据库。问题是 repo 为空,而不是执行 select 后的列表。 谁能帮帮我?
这是我的仓库:
@Repository
public interface UpcomingMatchesFactsRepository extends
CrudRepository<UpcomingMatchesFacts, Integer> {
public boolean existsByFirstPlayerName(String firstPlayerName);
@Query(value = "" +
"Select u " +
"from UpcomingMatchesFacts u " +
"order by abs(formula) desc ")
List<UpcomingMatchesFacts> getHighestFormulaMatches();
List<UpcomingMatchesFacts> findAll();
}
这是我的实体:
@Entity
@Table(name = "upcomingmatchesfacts")
public class UpcomingMatchesFacts {
public UpcomingMatchesFacts(String firstPlayerName, String secondPlayerName, int matchesWonFirstPlayer,
int matchesWonSecondPlayer, int currentRankFirstPlayer, int currentRankSecondPlayer,
int ageFirstPlayer, int ageSecondPlayer, double formula) {
this.firstPlayerName = firstPlayerName;
this.secondPlayerName = secondPlayerName;
this.matchesWonFirstPlayer = matchesWonFirstPlayer;
this.matchesWonSecondPlayer = matchesWonSecondPlayer;
this.currentRankFirstPlayer = currentRankFirstPlayer;
this.currentRankSecondPlayer = currentRankSecondPlayer;
this.ageFirstPlayer = ageFirstPlayer;
this.ageSecondPlayer = ageSecondPlayer;
this.formula = formula;
}
这是我要使用该方法的类:
@EnableJpaRepositories
public class ChooseBestMatches {
@Autowired
private UpcomingMatchesFactsRepository upcomingMatchesFactsRepository;
public void addBestMatchesToDatabase() {
List<UpcomingMatchesFacts> upcomingMatchesFacts = Lists.newArrayList(upcomingMatchesFactsRepository.findAll());
//upcomingMatchesFactsRepository.getHighestFormulaMatches().subList(0, 9);
}
}
【问题讨论】: