简体   繁体   中英

JPA Query get enum value

I've been going through many threads but none of them included an answer for my problem.

I got a single enum class:

public enum EActivationFunction {
  FKT_RELU("ReLU"),
  FKT_SIGMOID("Sigmoid"),
  FKT_TANH("Tanh"),
  FKT_LINEAR("Linear");

  @Getter private String name;

  EActivationFunction(final String name) {
    this.name = name;
  }
}

That is used in the normal ActivationFunction class:

  @Enumerated(EnumType.STRING)
  @Column(length = 20)
  private EActivationFunction type;

My query shortened:

 @Query(
      "SELECT f FROM MlpConfig f WHERE f.user = :user AND com.project.customMlp.domain.enumeration.EActivationFunction like %:searchQuery%")

So I tried full qualified name which does not work at all. When I query like this it works but not the name is used but the enum itself so FKT_RELU instead of relu:

lower(f.activationFunction.type)

ActivationFunction is another foreign entity of the main entity MlpConfig. How can I get the name? So normally:

f.activationFunction.type.name

In JPA, an Enum value can be mapped to its enum name or ordinal value. So the user-defined member variable of the enum type is invisible to JPA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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