简体   繁体   English

Spring 数据 JPA 使用@query 或 function 名称过滤枚举值集

[英]Spring Data JPA filter set of enum values with @query or by function name

I have a set of enums as an attribute in MyClass (which is an Entity) and want to filter the string values of the enum either with @Query or with the JPA function name querying (dont know the proper name for it...).我有一组枚举作为 MyClass 中的一个属性(它是一个实体),并希望使用 @Query 或 JPA function 名称查询过滤枚举的字符串值(不知道它的正确名称......) .

My Enum:我的枚举:

public enum MyEnum {
    ONE("First string value"),
    TWO("Second string value"),
    THREE("Third string value");

    private MyEnum (String name) {
       this.name = name;
    }
    private final String name;

    @Override
    public String toString() {
        return name;
    }
}

Declaration of the Set of Enums in the MyClass-Entity MyClass-Entity 中枚举集的声明

@Size(min=1)
@ElementCollection (fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
private Set<MyEnum> myenums;

The table it creates:它创建的表:

create table myclass_myenums (myclass_id bigint not null, myenum varchar(255));

@Query attempt in my Repository. @Query 在我的存储库中尝试。

@Query(value = "SELECT * from mytable t " +
            " left join myclass_myenums e ON e.myclass_id= t.id " +
            " WHERE " +
            " e.myenum LIKE CONCAT('%',:filtertext,'%') " 
            , nativeQuery = true)
    List<RisikoEntity> findbyFilter(@Param("filtertext") String filtertext);

This works, the problem is though, it filters on the Strings "ONE", "TWO", "THREE" and not on "First string value", "Second..." and so on.这行得通,但问题是,它过滤字符串“一”、“二”、“三”,而不是“第一个字符串值”、“第二个......”等等。


Alternative attempt with the function-name thing attempt in my Repository:在我的存储库中尝试使用函数名称的替代尝试:

List<MyClass> findByMyenumsContainsIgnoreCase(String filterstring)

---> Error: "Parameter value [%First%] did not match expected type [path.to.MyEnum (n/a)]"

Second attempt:第二次尝试:

List<MyClass> findByMyenums_NameContainsIgnoreCase(String filterstring)

--> Error: "Illegal attempt to dereference path source [null] of basic type"

What am I doing wrong?我究竟做错了什么?

  1. How can I filter the actual enum string-values in the first solution?如何在第一个解决方案中过滤实际的枚举字符串值?
  2. How can I filter sets of enum with the JPA function name thing?如何使用 JPA function 名称过滤枚举集?

Well I would say that this is not possible because JPA does not persist the name String of the enum but rather the actual name of the enum.好吧,我想说这是不可能的,因为 JPA 不保留枚举的name字符串,而是保留枚举的实际名称。

So your Database should contain ONE or TWO instead of First string value or Second string value inside myenum column.因此,您的数据库应该包含一个或两个,而myenum中的第一个字符串值第二个字符串值

The only way to solve this is to first filter the enum by code and then find by that enum解决此问题的唯一方法是首先按代码过滤枚举,然后按该枚举查找

If you use JPA 2.1, you may try to use its @Converter annotation as suggested here but this can create significant overhead as the converted String value will have to be stored in your entity:如果您使用 JPA 2.1,您可以尝试按照 此处的建议使用其@Converter注释,但这会产生大量开销,因为转换后的 String 值必须存储在您的实体中:

@Converter(autoApply = true)
public class MyEnumConverter implements AttributeConverter<MyEnum, String> {

    @Override
    public String convertToDatabaseColumn(MyEnum myEnum) {
        if (myEnum == null) {
            return null;
        }
        return myEnum.toString();
    }

    @Override
    public MyEnum convertToEntityAttribute(String code) {
        if (code == null) {
            return null;
        }

        return Stream.of(MyEnum.values())
          .filter(m -> m.toString().equals(code))
          .findFirst()
          .orElseThrow(IllegalArgumentException::new);
    }
}

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

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