首页 > 解决方案 > JPA query: Listing a entity based on boolean in sub-set

问题描述

So i got this Spring-boot backend and one of my entities looks like this:

@Entity
@Table( name = "users",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = "username"),
                @UniqueConstraint(columnNames = "email")
        })
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    @Size(max = 20)
    private String username;

    @NotBlank
    @Size(max = 50)
    @Email
    private String email;

    @NotBlank
    @Size(max = 120)
    private String password;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable( name = "user_roles",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles = new HashSet<>();

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<FileDB> files = new HashSet<>();

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private DeclarationOfHealth declarationOfHealth;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<CaseHistory> caseHistory = new HashSet<>();

Now The Entity "CaseHistory" has a boolean named "Finished" which indicates if the case is closed or not.

My Question is: Is there anyway to list Users with jpa query based on if the boolean Finished in CaseHistory is true. i tried List<User> findByCaseHistory_FinishedFalse();

but this gave me one user who had 4 open cases 4 times.

Thx in advance.

标签: jqueryspringspring-bootjpa

解决方案


推荐阅读