【发布时间】:2020-09-02 05:01:58
【问题描述】:
我是spring框架的初学者,
我在存储库和 SQL 连接部分面临一些问题。 我想使用连接查询从两个表中获取详细信息。
请在下方查找错误
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“commentController”的 bean 时出错:不满意 通过字段“joinRepository”表示的依赖关系;嵌套异常 是 org.springframework.beans.factory.BeanCreationException: 错误 创建名为“joinRepository”的 bean:调用 init 方法 失败的;嵌套异常是 java.lang.IllegalArgumentException: Not a 托管类型: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) [类/: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);
}
}
【问题讨论】:
标签: spring-boot spring-data-jpa