Java 正确使用单个@ManyToOne关联进行大小控制和分页

Java 正确使用单个@ManyToOne关联进行大小控制和分页,java,spring,hibernate,jpa,spring-data-jpa,Java,Spring,Hibernate,Jpa,Spring Data Jpa,我想避免“双向@OneToMany”关联,因为: 它不能限制@OneToMany 我需要分页 为此,我使用了“Just@ManyToOne”关联,但不幸的是,它只给出了一行与此相关的代码: List<PostComment> comments = entityManager.createQuery( "select pc " + "from PostComment pc " + "where pc.post.id = :postId", PostComment

我想避免“双向
@OneToMany
”关联,因为:

  • 它不能限制
    @OneToMany
  • 我需要分页 为此,我使用了“Just
    @ManyToOne
    ”关联,但不幸的是,它只给出了一行与此相关的代码:

    List<PostComment> comments = entityManager.createQuery(
        "select pc " +
        "from PostComment pc " +
        "where pc.post.id = :postId", PostComment.class)
    .setParameter( "postId", 1L )
    .getResultList();
    
    而这在
    帖子中

    private Set<Comment> comments; 
    

    因此,我如何使用“Just
    @manytone
    ”关联(或其他方便的方法)来控制
    注释的大小和分页?

    您可以通过使用
    Spring data
    JpaRepository
    来避免使用EntityManager。
    JpaRepository
    带有内置方法
    Page findAll(Pageable Pageable)
    ,可用于分页。您可以阅读本文开始学习:

    祝你好运

    编辑:另外,关于第4点:

    @ManyToOne(fetch = FetchType.LAZY)   
    @JoinColumn(name = "post_id")  
    private Post post; 
    

    这里你基本上是说一篇评论可以有很多帖子,而不是相反(一篇帖子可以有很多评论)。此外,如果不使用
    OneToMany
    也不能使用manytone。

    您可以通过使用
    Spring data
    JpaRepository
    来避免使用EntityManager。
    JpaRepository
    带有内置方法
    Page findAll(Pageable Pageable)
    ,可用于分页。您可以阅读本文开始学习:

    祝你好运

    编辑:另外,关于第4点:

    @ManyToOne(fetch = FetchType.LAZY)   
    @JoinColumn(name = "post_id")  
    private Post post; 
    

    这里你基本上是说一篇评论可以有很多帖子,而不是相反(一篇帖子可以有很多评论)。此外,如果不使用“一通”也不能使用“多通”。

    我发现这不是完美的,而是最适合我的解决方案

    职位:

    PostCommentServiceImpl:

    @Service
    public class PostCommentServiceImpl {
    
        @Autowired
        private PostCommentRepository repository;
    
        //...
    
        public void setCommentsInPost(Post post, int first, int size){
            Pageable pageRequest = new PageRequest(first, size);
    
            Page<PostComment> pageOfPostComment = repository.findByPostId(post.getId(), pageRequest);
    
            post.setComments(pageOfPostComment.getContent());
        }
    }
    

    我发现不是完美的,而是最适合我的解决方案

    职位:

    PostCommentServiceImpl:

    @Service
    public class PostCommentServiceImpl {
    
        @Autowired
        private PostCommentRepository repository;
    
        //...
    
        public void setCommentsInPost(Post post, int first, int size){
            Pageable pageRequest = new PageRequest(first, size);
    
            Page<PostComment> pageOfPostComment = repository.findByPostId(post.getId(), pageRequest);
    
            post.setComments(pageOfPostComment.getContent());
        }
    }
    

    这太宽了。你需要先了解你想做什么,然后阅读Spring和Spring数据jpa文档,学习jpa、JPQL,看看Spring数据jpa示例和教程,等等。这太宽泛了。您需要先了解您想要做什么,然后阅读Spring和Spring数据jpa文档,学习jpa、JPQL,查看Spring数据jpa示例和教程等。感谢您的回答!哎呀,在4分上犯了个愚蠢的错误。关于联想,我有两个问题:1。你说过“如果不使用OneToMany,你就不能使用ManyTone”,但是“Just@manyTone”的意思呢?2.此外,您建议使用
    ManyToOne
    OneToMany
    ,但提到的教程说“您不能限制@OneToMany集合的大小”,那么我可以用哪种方式来使用你的建议呢?你应该问问文章的作者,他对
    的意思是什么,孩子端的@ManyToOne注释就是你所需要的一切。
    。据我所知,没有一个域名,你就不能使用一个域名。但是,您可以使用@OneToMany而不使用@ManyToOne。那么这是一种单向关系。顺便问一下,如果答案回答了你的问题,你能接受吗?对不起!我完全忘记了这个帖子。之后我找到了一些解决方案,你可以检查一下。无论如何,谢谢!谢谢你的回答!哎呀,在4分上犯了个愚蠢的错误。关于联想,我有两个问题:1。你说过“如果不使用OneToMany,你就不能使用ManyTone”,但是“Just@manyTone”的意思呢?2.此外,您建议使用
    ManyToOne
    OneToMany
    ,但提到的教程说“您不能限制@OneToMany集合的大小”,那么我可以用哪种方式来使用你的建议呢?你应该问问文章的作者,他对
    的意思是什么,孩子端的@ManyToOne注释就是你所需要的一切。
    。据我所知,没有一个域名,你就不能使用一个域名。但是,您可以使用@OneToMany而不使用@ManyToOne。那么这是一种单向关系。顺便问一下,如果答案回答了你的问题,你能接受吗?对不起!我完全忘记了这个帖子。之后我找到了一些解决方案,你可以检查一下。无论如何,谢谢!
    @Entity(name = "PostComment")
    public class PostComment {
    
        //...
    
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "post_id")
        private Post post;
    
        public Post getPost() {
            return post;
        }
    
        public void setPost(Post post) {
            this.post = post;
        }
    }
    
    @Service
    public class PostCommentServiceImpl {
    
        @Autowired
        private PostCommentRepository repository;
    
        //...
    
        public void setCommentsInPost(Post post, int first, int size){
            Pageable pageRequest = new PageRequest(first, size);
    
            Page<PostComment> pageOfPostComment = repository.findByPostId(post.getId(), pageRequest);
    
            post.setComments(pageOfPostComment.getContent());
        }
    }
    
    @Controller
    public class PostController {
        @Autowired
        private PostCommentService postCommentService;
    
        @Autowired
        private PostService postService;
    
        //...
        @RequestMapping(value = "/something", method = RequestMethod.GET)
        public void foo() {
            Post post = postService.findById(1L);
    
            postCommentService.setCommentsInPost(post,0,10);
    
            //...
        }
        //...
    }