有没有办法在 Spring Data @Query 注释值中使用常量?
我不想硬编码常量值,我宁愿通过引用变量来指定它们.
I don't want to hardcode constant values, I would rather specify them through a reference variable.
例如,而不是编写下一个查询:
For example, rather then writing the next query:
@Query(value = "SELECT u FROM UserModel u WHERE u.status = 1")
..我想提取硬编码值 '1' 并编写如下内容:
..I would like to extract the hardcoded value '1' and write something like:
@Query(value = "SELECT u FROM UserModel u WHERE u.status = UserModel.STATUS_ACTIVE") //doesn't compile
有没有办法像第二个例子那样在 spring-data 查询中指定常量?
Is there a way to specify constants like in the second example inside spring-data queries?
推荐答案
你必须像这样使用完全限定的类名:
You have to use fully qualified class name like this:
@Query("SELECT u FROM UserModel u WHERE u.status = com.example.package.UserModel.STATUS_ACTIVE")
不过,它的坏处是 IDE 不会将其识别为对 UserModel 类的使用.唯一的好处是您可以将值保存在一个地方,这在大多数情况下就足够了. 这已在 IntelliJ IDEA 2017.1.我不知道其他 IDE.
The bad thing about it though is that an IDE would not recognise this as an usage of the class UserModel. The only advantage is that you can keep the value in one place, which is sufficient most of the time. This has been resolved in IntelliJ IDEA 2017.1. I don't know about other IDEs.
相关文章