我是Spring框架的初学者,
我在存储库和SQL连接部分遇到了一些问题。 我想使用连接查询从两个表中获取详细信息。
请在错误下方查找
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为'commentController'的bean时出错:不满意 通过字段“ joinRepository”表示的依赖关系;嵌套异常 是org.springframework.beans.factory.BeanCreationException:错误 创建名称为“ joinRepository”的bean:初始化方法的调用 失败嵌套异常是java.lang.IllegalArgumentException:不是 托管类型:com.example.jpa.model.JoinQuery类,位于 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:323) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895) 〜[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) 〜[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 〜[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) 〜[spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]位于 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]位于 org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]位于 org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]位于 org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]位于 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
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
}
控制器:
@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);
}
}
答案 0 :(得分:0)
public class JoinQuery
需要@Entity
注释。
@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
}
该查询也不应该是本机查询使用:
@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:请勿将Entity用作控件的返回值。了解3tier架构