简体   繁体   English

JPA条件,将枚举类型转换为字符串

[英]Jpa criteria, convert enum type to string

i got some problem. 我有问题。 I created entity: 我创建了实体:

@Entity 
public class Trace extends AbstractEntity {

@Column(name = "trace_level")
@Enumerated(EnumType.STRING)
private TraceLevel traceLevel;

...
}

And jpa criteria: 和jpa标准:

List<String> tempList = new ArrayList<>();
tempList.add("Warning");
Path path = root.get("traceLevel");
Predicate inP = path.in(tempList);

Of course i got problem 我当然有问题

Parameter value [Warning] did not match expected type [com.getknowledge.platform.modules.trace.trace.level.TraceLevel (n/a)]

Since the data for the filter come from the client, and are processed at the basic level, the transformation in string to enum i can not do. 由于过滤器的数据来自客户端,并在基本级别上进行处理,因此无法将字符串转换为枚举i。 The question is, can i build query using strings. 问题是,我可以使用字符串构建查询吗?

Every enum has a function valueOf that takes a String and returns the appropriate constant. 每个enum都有一个函数valueOf ,该函数采用String并返回适当的常量。 So instead of 所以代替

List<String> tempList = new ArrayList<>();
tempList.add("Warning");

use 采用

List<TraceLevel> tempList=new ArrayList<>();
tempList.add(TraceLevel.valueOf("Warning"));

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

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