Is there a way to use constants inside Spring Data @Query annotation value?(有没有办法在 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.
这篇关于有没有办法在 Spring Data @Query 注释值中使用常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法在 Spring Data @Query 注释值中使用常量?
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
