I searched on SO for similar questions, however I did not find very specific answer.
I am new to Springboot. I have a defined a POJO in service layer. I want to inject repository into this class. Somehow, all the time it comes out to be null
. Here is my code structure,
file : service/ResultInstitute.java
@Document(indexName = "result_institute")
public class ResultInstitute implements Serializable {
@Inject
public CourseRepository courseRepository;
/**
*
*/
private static final long serialVersionUID = -2168910694195614091L;
public ResultInstitute(Institute institute) {
// Initialize all the values.
for (Course course : institute.getCourses()){
HashMap<String, String> courseDetails = courseRepository.getCourseDetails(course.getId());
course.setCourseDetails(courseDetails);
courses.add(course);
}
courses = institute.getCourses();
for (Course course : courses){
subCategories.put(course.getSubCategory().getId(), course.getSubCategory().getDisplayName());
categories.put(course.getSubCategory()
.getCategory()
.getId(),
course.getSubCategory()
.getCategory()
.getDisplayName());
}
}
public ResultInstitute (){}
private Long id;
private String code; ....
file : repository/CourseRepository.java
public interface CourseRepository extends JpaRepository<Course,Long> {
@Query("select distinct course from Course course left join fetch course.institutes")
List<Course> findAllWithEagerRelationships();
@Query("select course from Course course left join fetch course.institutes where course.id =:id")
Course findOneWithEagerRelationships(@Param("id") Long id);
@Query(value="SELECT DISTINCT(ci.course_details) FROM course_institute ci WHERE ci.courses_id = ?1", nativeQuery = true)
HashMap<String, String> getCourseDetails(Long id);
}
Whenever I'm trying to use courseRepository
it gives me NullPointerException
. Can you please help me with this.