0
votes

I am a Beginner to spring framework,

I am facing some issue on Repository and SQL connection part. I want to fetch details from two tables using join query.

Please find below the error

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'commentController': Unsatisfied dependency expressed through field 'joinRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'joinRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.jpa.model.JoinQuery at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE] at com.example.jpa.JpaOneToManyDemoApplication.main(JpaOneToManyDemoApplication.java:14) [classes/:na]

select c.created_at , c.updated_at , p.content,c.text , p.id , c.posting_dummy 
from     post p join  comment c 
on  p.id = c.posting_dummy;

Repository :

@Repository
public interface JoinRepository  extends JpaRepository<JoinQuery, Long>
{
@Query(value = " select  c.created_at , c.updated_at , p.content,c.text , p.id , c.posting_dummy "
            + "  from   post p join  comment c   on  p.id = :id ", nativeQuery = true)
public  List<JoinQuery> queryBy(Long id);
}

POJO:

public class JoinQuery 
{
private Long id;
private Date createdAt;
private Date updatedAt;
private String content;
private String text;
private  Post posting_dummy;

// Getter and Setter for fields       
}

Controller :

@RestController
public class CommentController {

@Autowired
private CommentRepository commentRepository;

@Autowired
private PostRepository postRepository;

@Autowired
private JoinRepository joinRepository;

@GetMapping("/posts/{postId}/comments")
public List<JoinQuery>  getAllCommentsByPostId(@PathVariable (value = "postId") Long postId
                                                    ) 
{
System.out.println("hi am here ");
return joinRepository.queryBy(postId);
}

}
1

1 Answers

0
votes

public class JoinQuery need the @Entity annotation.

@Entity
public class JoinQuery 
{
private Long id;
private Date createdAt;
private Date updatedAt;
private String content;
private String text;
private  Post posting_dummy;

// Getter and Setter for fields       
}

Also the query sould not be a native query use:

@Query(value = " select  new <package of jpaquery>.JoinQuery(c.created_at , c.updated_at , p.content,c.text , p.id , c.posting_dummy) "
            + "  from   post p.c")

BTW: Do not use an Entity as a return value of a controler. Learn about 3tier architecture