【问题标题】:Spring JPA Join春季 JPA 加入
【发布时间】: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


    【解决方案1】:

    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")
    

    顺便说一句:不要使用实体作为控制器的返回值。了解 3tier 架构

    【讨论】:

    • 嗨 Jens 实际上我有两个不同的实体一个是发布另一个是评论....通过这两个实体我想实现连接查询(这就是我放置 POJO 类(JoinQuery)的原因)
    • 大家好,请在此分享意见.....等待您的回复
    猜你喜欢
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2013-11-12
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多