简体   繁体   English

JpaRepository查找子项(可分页)

[英]JpaRepository find child (pageable)

I came across a problem that I can not solve. 我遇到了一个我无法解决的问题。

I want to find Class1 elements with "Active" status, which have "Active" status2 on Class2 and belong to a specific user . 我想找到具有“活动”状态的Class1元素,这些status2Class2上具有“活动” status2 ,并且属于特定user

Eg: Find all "Accepted" comments, which belong to "John" posted on post with status "Active". 例如:查找所有张贴在帖子上且状态为“有效”的“已接受”评论,这些评论属于“约翰”。

I tried to use this (JpaRepository): 我尝试使用此(JpaRepository):

Page<Class1> findAllByStatusAndClass2StatusAndClass2User(String status, String status2, User user, Pageable pageRequest)

My code: 我的代码:

@Entity
@Table(name = "class1")
public class Class1 {
...

    @OneToMany(mappedBy = "class1")
    private Set<Class2> class2;

    String status1;
...
}

@Entity
@Table(name = "class2")
public class Class2 {
...

    @ManyToOne
    @JoinColumn(name = "class1_id", nullable = false)
    private Class1 class1;

    User user;       
    String status2;
...
}

Thank you for help. 谢谢你的帮助。

In fact with JPARepository, your method name and params should respect the fields names in your entities. 实际上,对于JPARepository,方法名称和参数应遵循实体中的字段名称。

So you need to use status1 instead of status and Class2Status2 instead of Class2Status . 所以,你需要使用status1 ,而不是statusClass2Status2代替Class2Status

Your method should be like this: 您的方法应如下所示:

Page<Class1> findAllByStatus1AndClass2Status2AndClass2User(String status1, String status2, User user, Pageable pageRequest)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM