Reputation: 1956
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.
Upvotes: 0
Views: 2770
Reputation: 734
In the comment section you said that your initiating the ResultInstitute class as follows,
ResultInstitute resultInstitute = new ResultInstitute(i); resultInstitute.locationFilterString(i);
The initiation is handled by yourself, so @Autowired will not work in this case. @Autowired is a spring configuration to inject the beans, so inorder to inject Repository ResultInstitute should be handled by Spring Iteself.
We have to tell spring that ResultInstitute is bean class for that you can annotate ResultIntitute class as @Component
@Component
public class ResultInititute
So whenever you wanted to instantiate ResultInstitute, instiate as a bean class using @Autowired
@Autowired
ResultInstitute resultInstitute;
And Using the Respository in Entity class is not good thing, we have to handle this in separate section.
Upvotes: 1
Reputation: 3582
Add it to your constructor @Document(indexName = "result_institute") public class ResultInstitute implements Serializable {
public CourseRepository courseRepository;
@Autowired
public ResultInstitute (Institute institute, CourseRepository courseRepository) {
this.courseRepository = courseRepository;
}
}
Upvotes: 0
Reputation: 85
You need to annotate your ResultInstitute
with @Component
. This tells spring that it has to inject dependencies.
Upvotes: 0