Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enum in @Query as a constant

I have tried to put a full class path (com.xxxx.State.Finish) after != but not helping.

@Query("select c from CustomOrder c where c.dealer = :roleName and 
     c.nextManager = null and c.currentState != Finish")
List<CustomOrder> findOpenOrder(@Param("roleName") String roleName);

Entity:

@Getter
@Enumerated(EnumType.STRING)
CustomOrderEnums.State currentState;

Enum:

public enum State {
    Open, Finish
}
like image 585
Tiina Avatar asked Dec 12 '17 08:12

Tiina


People also ask

How do you pass enum in Request Param?

Use Enums as Request Parameters Or we can use it as a PathVariable: @GetMapping("/findbymode/{mode}") public String findByEnum(@PathVariable("mode") Modes mode) { // ... } When we make a web request, such as /mode2str? mode=ALPHA, the request parameter is a String object.

How do you use enum in DTO?

You can use the name() or toString() method of your Enum . You can provide an additional "translation" inside the Enum like: public enum Status { S1("translation1"), S2("translation2"), S3("translation3"); private final String translation; private Status(String t) { translation = t; } ... }


1 Answers

@Query("select c from CustomOrder c where c.dealer = :roleName and 
     c.nextManager = null and c.currentState != com.xxx.FooEnum.Finish")

FooEnum has to be a top class not an inner one. If it has to be an inner class, use ' quoted string (haven't tried it without ').

@Query("select c from CustomOrder c where c.dealer = :roleName and 
     c.nextManager = null and c.currentState != 'Finish'")

I have just found that instead of using @Query it could be simply used as:

List<User> findIdByRoleRoleAndProvinceType(String role, ProvinceEnum.ProvinceType provinceType);

and this is entity User:

@Entity
public class User {
    Role role; // entity has a String field role;
    Province province; // entity has a ProvinceEnum.ProvinceType field type.
...
}
like image 68
Tiina Avatar answered Sep 26 '22 19:09

Tiina