枚举与查询中使用的方法未转换

huangapple go评论90阅读模式
英文:

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 = &quot;operation_Sequence&quot;)
    @SequenceGenerator(name = &quot;operation_Sequence&quot;, sequenceName = &quot;operation_Sequence&quot;, 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 = &quot;select ce from Operation ce where &quot;
            + &quot;(ce.eventProcessStatus=com.ns.perma.domain.constants.EventProcessStatus.CLOSE.getId() )&quot;)
public List&lt;Operation&gt; 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 = &quot;select ce from Operation ce where &quot;
            + &quot;ce.eventProcessStatus
= :#{ T(com.ns.perma.domain.constants.EventProcessStatus).CLOSE.id &quot;)
public List&lt;Operation&gt; 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 = &quot;select ce from Operation ce where ce.eventProcessStatus= ?1&quot;)
public List&lt;Operation&gt; findAllOperation(int enumValue);

Then call this function using enum value

operationRepo.findAllOperation(EventProcessStatus.CLOSE.getId());

huangapple
  • 本文由 发表于 2020年4月7日 22:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61082902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定