英文:
Enum with method used in a query is not converted
问题
我使用 Spring Boot 和 Spring Data JPA。
我有一个整数数据类型的字段。
我有一个枚举,为这个字段具有不同的值。
public class Operation{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "operation_Sequence")
@SequenceGenerator(name = "operation_Sequence", sequenceName = "operation_Sequence", allocationSize = 1)
private Long id;
private Integer eventProcessStatus;
}
public enum EventProcessStatus {
CLOSE(2),
OPEN(99);
private final int id;
EventProcessStatus(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
在我的 Repository 中,我希望使用 getId 方法来使用这个枚举。
@Query(value = "select ce from Operation ce where "
+ "(ce.eventProcessStatus = com.ns.perma.domain.constants.EventProcessStatus.CLOSE.getId())")
public List<Operation> findAllOperation();
当执行这个查询时,我得到 CLOSE: INVALID IDENTIFIER。
在 SQL 日志中,我看到
...
where
operation0_.event_process_status = com.ns.perma.billing.domain.constants.EventProcessStatus.SUCCESS.getId()
因此该命令未被转换。
有任何想法吗?
英文:
I use spring boot with spring data jpa
I have a field with a integer data type.
I have an enum with different value for this field
public class Operation{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "operation_Sequence")
@SequenceGenerator(name = "operation_Sequence", sequenceName = "operation_Sequence", allocationSize = 1)
private Long id;
private Integer eventProcessStatus;
}
public enum EventProcessStatus {
CLOSE(2),
OPEN(99);
private final int id;
EventProcessStatus(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
In my repository, I search to use this enum with the getId method
@Query(value = "select ce from Operation ce where "
+ "(ce.eventProcessStatus=com.ns.perma.domain.constants.EventProcessStatus.CLOSE.getId() )")
public List<Operation> findAllOperation();
When this query is executed, I get CLOSE: INVALID IDENTIFIER.
In the sql log I see
...
where
operation0_.event_process_status=com.ns.perma.billing.domain.constants.EventProcessStatus.SUCCESS.getId()
So the command is not converted.
Any idea?
答案1
得分: 2
你不能在JPQL查询中使用任意的Java代码片段。
但是,你可以在查询注解中使用SpEL表达式。请注意,你需要使用特殊的T
运算符来访问静态成员。因此,以下代码(或类似的代码)应该可以工作:
@Query(value = "select ce from Operation ce where "
+ "ce.eventProcessStatus = :#{ T(com.ns.perma.domain.constants.EventProcessStatus).CLOSE.id }")
public List<Operation> findAllOperation();
英文:
You can't use arbitrary Java snippets in a JPQL query.
But you may use SpEL expressions in a query annotation.
Just take note that you need to use the special T
operator to access static members. Therefore the following (or something similar to it) should work:
@Query(value = "select ce from Operation ce where "
+ "ce.eventProcessStatus
= :#{ T(com.ns.perma.domain.constants.EventProcessStatus).CLOSE.id ")
public List<Operation> findAllOperation();
答案2
得分: 1
@JensSchauder 是对的。您也可以尝试这种方式。
您可以将枚举值用作参数并传递到查询中:
@Query(value = "select ce from Operation ce where ce.eventProcessStatus = ?1")
public List<Operation> findAllOperation(int enumValue);
然后使用枚举值调用此函数:
operationRepo.findAllOperation(EventProcessStatus.CLOSE.getId());
英文:
@JensSchauder is right. You can try this way also.
You can use enum value as parameter and pass in the query
@Query(value = "select ce from Operation ce where ce.eventProcessStatus= ?1")
public List<Operation> findAllOperation(int enumValue);
Then call this function using enum value
operationRepo.findAllOperation(EventProcessStatus.CLOSE.getId());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论