1

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?

Eklavya
  • 17,618
  • 4
  • 28
  • 57
robert trudel
  • 5,283
  • 17
  • 72
  • 124

2 Answers2

2

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();
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
1

@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());
Eklavya
  • 17,618
  • 4
  • 28
  • 57