Reputation: 5749
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?
Upvotes: 1
Views: 516
Reputation: 18430
@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());
Upvotes: 1
Reputation: 81907
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();
Upvotes: 2