2017-07-26 500 views
0

我有一个Spring-Boot API,带有下面的端点。它在Spring Data JPA findAll查询中抛出一个空指针异常;当我评论这一行时,我没有任何错误。看来,我从存储库查询中得到一个空结果,但我知道直接从数据库查询数据。我不明白为什么我得到一个空的topicsLookup变量...任何人都可以指出我在正确的方向吗?Spring Data JPA Repository findAll()空指针

资源:

@RequestMapping(value = "/lectures/{lectureId}", 
     method = RequestMethod.GET, 
     produces = MediaType.APPLICATION_JSON_VALUE) 
public Map<String, SpeakerTopicLectures> getLecture(@PathVariable Long lectureId){ 

     Long requestReceived = new Date().getTime(); 
     Map<String, SpeakerTopicLectures> result = new HashMap<>(); 

     log.debug("** GET Request to getLecture"); 
     log.debug("Querying results"); 

     List<SpeakerTopicLectures> dataRows = speakerTopicLecturesRepository.findBySpeakerTopicLecturesPk_LectureId(lectureId); 

     // This line throws the error 
     List<SpeakerTopic> topicsLookup = speakerTopicsRepository.findAll(); 

     // Do stuff here... 

     log.debug("Got {} rows", dataRows.size()); 
     log.debug("Request took {}ms **", (new Date().getTime() - requestReceived)); 

     // wrap lecture in map object 
     result.put("content", dataRows.get(0)); 

     return result; 
} 

的Java Bean:

@Entity 
@Table(name = "speaker_topics") 
@JsonInclude(JsonInclude.Include.NON_NULL) 
@Data 
public class SpeakerTopic implements Serializable { 

    @Id 
    @Column(name = "topic_id") 
    private Long topicId; 

    @Column(name = "topic_nm") 
    private String topicName; 

    @Column(name = "topic_desc") 
    private String topicDesc; 

    @Column(name = "topic_acm_relt_rsce") 
    private String relatedResources; 

} 

库:

import org.acm.dl.api.domain.SpeakerTopic; 
import org.springframework.data.jpa.repository.JpaRepository; 

public interface SpeakerTopicsRepository extends JpaRepository<SpeakerTopic,Long> { 

} 
+0

如果有更多信息会有帮助,请告诉我,我会尽量提供。 – littlewolf

+2

堆栈跟踪可能会有所帮助。你能确认speakerTopicsRepository本身是否为空 - 你有@Autowired吗? – NickJ

+0

@NickJ就是这样。我忘了'@ Inject'注释。随意发布作为答案,我会标记它。 – littlewolf

回答

1

最有可能的CA使用的是speakerTopicsRepository本身是空的,这可能是由于忘记自动装载而导致的,例如,

public class YourController { 

    @Autowired private SpeakerTopicsRepository speakerTopicsRepository; 

    @RequestMapping(value = "/lectures/{lectureId}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    public Map<String, SpeakerTopicLectures> getLecture(@PathVariable Long lectureId) { 
    // your method... 
    } 

} 
相关问题