[英]Spring Data JPA Repository findAll() Null Pointer
我有一个带有以下端点的 Spring-Boot API。 它在 Spring Data JPA findAll
查询上抛出空指针异常; 当我注释掉这一行时,我没有收到任何错误。 似乎我从存储库查询中得到了一个空结果,但我知道数据是通过直接查询数据库获得的。 我不明白为什么我的topicsLookup
变量为空...谁能指出我正确的方向?
资源:
@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;
}
爪哇豆:
@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> {
}
最可能的原因是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...
}
}
存储库不会在您的控制器中自动装配。
尝试使用
@Repository
@Transactional
public interface SpeakerTopicsRepository extends JpaRepository<SpeakerTopic,Long> {
// Your Repository Code
}
我认为缺少@Repository
和@Transactional
。 请使用它。
它对我来说缺少@Autowired。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.