我实现了一个存储库,一个实体和一个POJO,我想从存储库中调用findAll()方法,但是即使填充了数据库,它也会返回空指针异常。问题在于,存储库为空,而不是执行选择后的列表。 谁能帮我吗?
这是我的仓库:
@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);
}
}
答案 0 :(得分:1)
如果您想将Spring bean注入其他类,它也必须是spring bean。在ChooseBestMatches类上使用@Service批注。
建议:@EnableJpaRepositories应该在Configuation类上,而不在服务上。 实体必须具有noarg构造函数。