如何在@Query中使用枚举作为常量

时间:2017-12-12 08:00:27

标签: spring-data-jpa

我试图在!=之后放一个完整的类路径(com.xxxx.State.Finish)但没有帮助。

@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);

实体:

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

枚举:

public enum State {
    Open, Finish
}

1 个答案:

答案 0 :(得分:0)

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

FooEnum必须是顶级而非内心的。如果它必须是内部类,请使用'引用的字符串(避免在没有'的情况下尝试过它)。

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

我刚刚发现,它不是使用@Query,而是可以简单地用作:

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

这是实体用户:

@Entity
public class User {
    Role role; // entity has a String field role;
    Province province; // entity has a ProvinceEnum.ProvinceType field type.
...
}