枚举的 JPA 规范
JPA Specification for Enum
我的 JPA 实体作为枚举字段
@Table(name="zsrb_ordini_prod")
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class OrdineProd implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long idOrdineProd;
...
@Enumerated(EnumType.STRING)
public StOrdine stato = StOrdine.CREATO;
...
}
在哪里
public enum StOrdine {
CREATO,
SCHEDULATO,
CONFERMATO,
SCARTATO
}
如果我添加一个带有规范的 where 条件
Specification<OrdineProd> = (root, query, qb)->
qb.equal(root.get("stato"), StOrdine.SCHEDULATO);
java.lang.IllegalArgumentException: Parameter value [SCHEDULATO] did not match expected type [imp.srb.progettazione.ordProd.StOrdine (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
...
将枚举包含到规范查询中的正确方法是什么?
尝试这样做:
Specification<OrdineProd> = (root, query, qb) ->
qb.equal(root.get("stato"), StOrdine.SCHEDULATO.name);
我的 JPA 实体作为枚举字段
@Table(name="zsrb_ordini_prod")
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class OrdineProd implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long idOrdineProd;
...
@Enumerated(EnumType.STRING)
public StOrdine stato = StOrdine.CREATO;
...
}
在哪里
public enum StOrdine {
CREATO,
SCHEDULATO,
CONFERMATO,
SCARTATO
}
如果我添加一个带有规范的 where 条件
Specification<OrdineProd> = (root, query, qb)->
qb.equal(root.get("stato"), StOrdine.SCHEDULATO);
java.lang.IllegalArgumentException: Parameter value [SCHEDULATO] did not match expected type [imp.srb.progettazione.ordProd.StOrdine (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
...
将枚举包含到规范查询中的正确方法是什么?
尝试这样做:
Specification<OrdineProd> = (root, query, qb) ->
qb.equal(root.get("stato"), StOrdine.SCHEDULATO.name);