Java Spring数据JPA存储库findAll()空指针
我有一个Spring引导API,其端点如下。它在Spring数据JPAJava Spring数据JPA存储库findAll()空指针,java,hibernate,spring-boot,spring-data-jpa,Java,Hibernate,Spring Boot,Spring Data Jpa,我有一个Spring引导API,其端点如下。它在Spring数据JPAfindAll查询上抛出空指针异常;当我注释掉这一行时,没有错误。似乎我从存储库查询中得到了一个空结果,但我通过直接查询数据库知道数据在那里。我无法理解为什么topicsLookup变量的值为空。。。谁能给我指出正确的方向吗 资源: @RequestMapping(value = "/lectures/{lectureId}", method = RequestMethod.GET, produ
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> {
}
存储库:
@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> {
}
import org.acm.dl.api.domain.SpeakerTopic;
导入org.springframework.data.jpa.repository.JpaRepository;
公共接口SpeakerTopicsRepository扩展了JpaRepository{
}
最有可能的原因是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...
}
}
公共类控制器{
@自动连线的私人通话记录存储通话记录存储;
@RequestMapping(value=“/teachs/{teacheid}”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
公共地图GetTarchy(@PathVariable Long-Tarchy){
//你的方法。。。
}
}
最有可能的原因是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...
}
}
公共类控制器{
@自动连线的私人通话记录存储通话记录存储;
@RequestMapping(value=“/teachs/{teacheid}”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
公共地图GetTarchy(@PathVariable Long-Tarchy){
//你的方法。。。
}
}
尝试使用
@Repository
@Transactional
public interface SpeakerTopicsRepository extends JpaRepository<SpeakerTopic,Long> {
// Your Repository Code
}
@存储库
@交易的
公共接口SpeakerTopicsRepository扩展了JpaRepository{
//您的存储库代码
}
我认为@Repository
&@Transactional
缺失。请用它 尝试使用
@Repository
@Transactional
public interface SpeakerTopicsRepository extends JpaRepository<SpeakerTopic,Long> {
// Your Repository Code
}
@存储库
@交易的
公共接口SpeakerTopicsRepository扩展了JpaRepository{
//您的存储库代码
}
我认为
@Repository
&@Transactional
缺失。请用它 存储库未在您的控制器中自动连接。存储库未在您的控制器中自动连接。它缺少@autowired for me。它缺少@autowired for me。如果有更多信息需要帮助,请告诉我,我将尝试提供它。stacktrace可能会有所帮助。你能确认speakerTopicsRepository本身不是空的吗?你是否“自动连线”过它?@NickJ就是它。我忘记了@Inject
注释。请随意发布作为答案,我会标记它。如果任何更多的信息将有帮助,请让我知道,我会尝试提供它。stacktrace可能会有所帮助。你能确认speakerTopicsRepository本身不是空的吗?你是否“自动连线”过它?@NickJ就是它。我忘记了@Inject
注释。请随意发布答案,我会标记它。请花一点时间阅读中的。堆栈溢出的格式与其他站点不同。请花点时间阅读中的。堆栈溢出上的格式设置与其他站点不同。