How to use EnumSets in a Repository

37 Views Asked by At

I have an EnumSet in a JPA entity.

@Entity
public class MyRecord
{   
    public static enum Flag { F1, F2, F3 };
    
    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Long id;        
    
    @NotNull
    @Column (unique = false)
    private String title;

    @ElementCollection
    @CollectionTable (name = "flags", joinColumns = @JoinColumn (name = "log_record_id"))
    @Column (name = "flag")
    @Enumerated (EnumType.STRING)
    private Set <Flag> flags;
}

This works as expected.

I also have a Repository for using it beside the standard methods.

public interface MyRepo extends JpaRepository <MyRecord, Long> 
{
  List <MyRecord> findByTitle (String title);
}

Also OK.

But I do not get it going with the EnumSet.

I tried these without success.

List <MyRecord> findByFlags (Set <MyRecord.Flag> es);

List <MyRecord> findByFlags (EnumSet <MyRecord.Flag> es);

This is the exception.

Operator SIMPLE_PROPERTY on flags requires a scalar argument, found interface java.util.Set in method public abstract java.util.List MyRepo.findByFlags(java.util.Set)

Are EnumSets not supported by the generic CRUD interfaces of JPA?

0

There are 0 best solutions below