Search code examples
javasqlspring-bootjpa

Is there a way to filter with JPA using a Set of IDs?


public class ProcessEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    @NotNull
    private String processDefinitionName;
    @Column(name = "groups_ids")
    @ElementCollection
    private Set<Long> groupIdsProcess = new HashSet<>();
}

public class ProcessInstance implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    @ManyToOne
    private ProcessEntity processEntity;
}

I want to filter ProcessInstance using JPA by using a method like:

List<ProcessInstance> getAllByProcessEntityGroupIdsProcessContains(Set<Long> setOfIDs) 

That will return a list of ProcessInstances whose processEntity.groupIdsProcess contains any of the IDs contained in the passed parameter Set<Long> setOfIDs

Is it possible?

I tried writing a native query without success, help would be appreciated


Solution

  • You can write a JPA find method, for your example:

    findAllByProcessEntity_GroupIdsProcessIsIn(Set<Long> setOfIDs)
    

    I am not sure about the groupIdsProcess part, that is annotated with @ElementCollection, but missing a @CollectionTable(...) annotation, does it correctly map and return every value if you use findAll() in the ProcessInstance Repository?