I'm trying to run some sample code, where I'm just trying to simply test whether I can extract data from table which is diff from mentioned in JpaRepository<Post, Long>
For example, In below code, repository managing Post entity, but I'm trying to get data from other table as you can see in getAllComments() method. Is there any issue using like this ? Or Its mandatory to get data from only table which is mentioned in JpaRepository i.e Post ?
public interface DemoRepository extends JpaRepository<Post, Long>{
@Query("Select * from postcomment")
List<Comment> getAllComments();
}
Secondly, when I'm trying to call this repo in my main class, its not able to instantiate. Any reason why ?
@SpringBootApplication
public class Demo {
@Autowired
public DemoRepository demoRepo;
public static void main(String[] args) {
SpringApplication.run(Demo.class, args);
List<Comment> comment = new Demo().demoRepo.getAllComments();
System.out.println(comment.get(0).getRemark());
}
}
getting error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo': Unsatisfied dependency expressed through field 'demoRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoRepository' defined in com.rest.demo.DemoRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.rest.api.entity.Post
How to ensure, everything is loaded by spring And its ready to be used in @Autowired annotations ?