Java 有没有办法在 Spring Data @Query 注释值中使用常量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18402988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:17:23  来源:igfitidea点击:

Is there a way to use constants inside Spring Data @Query annotation value?

javaspringjpqlspring-data

提问by Roman

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")

..I would like to extract the hardcoded value '1' and write something like:

..我想提取硬编码值“1”并编写如下内容:

@Query(value = "SELECT u FROM UserModel u WHERE u.status = UserModel.STATUS_ACTIVE")  //doesn't compile

Is there a way to specify constants like in the second example inside spring-data queries?

有没有办法在 spring-data 查询中的第二个示例中指定常量?

回答by Kevin Bowersox

I would recommend creating an Enumand a field of that enum on the entity.

我建议Enum在实体上创建该枚举的一个和一个字段。

public enum UserModelStatus{
     ACTIVE, INACTIVE
}

public UserModel{

    /* Other fields ommitted */

    @Enumerated(EnumType.STRING)
    private UserModelStatus status;

    /* Get/Set Method */
}

Then create your repository method:

然后创建您的存储库方法:

@Repository
public interface UserModelRepository extends JpaRepository<UserModel, Long>{

    public List<UserModel> findByStatus(UserModelStatus status);

}

Using Spring Data you won't even need to write JPQL just call the method like:

使用 Spring Data,您甚至不需要编写 JPQL 只需调用如下方法:

   @Autowired
   UserModelRepository userModelRepository;

   public void someMethod(){
       List<UserModel> userModels = userModelRepository.findByStatus(UserModelStatus.ACTIVE);
   }

回答by 0x7d7b

When you want to use constants directly inside your @Query annotation you can write something like:

当您想直接在 @Query 注释中使用常量时,您可以编写如下内容:

@Query("SELECT u FROM UserModel u WHERE u.status = " + UserModel.STATUS_ACTIVE)

回答by Cleankod

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")

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.

但它的坏处是 IDE 不会将此识别为 UserModel 类的用法。唯一的优点是您可以将值保存在一个地方,这在大多数情况下就足够了。这已在IntelliJ IDEA 2017.1 中解决。我不知道其他 IDE。

回答by cmorris

The answer to this seems to be 'No' for a standard solution.

对于标准解决方案,这个问题的答案似乎是“否”。

Some JPA implementations may have solutions of their own but hibernate for one does not seem to and does not seem to support any of the methods suggested by other answers here.

一些 JPA 实现可能有自己的解决方案,但一个人的休眠似乎不支持,也不支持此处其他答案建议的任何方法。

回答by zdesam

Use as follows:

使用方法如下:

In the repository interface, define a constant as follows:

在存储库界面中,定义一个常量如下:

public static final String USER_QUERY = "SELECT u FROM UserModel u WHERE u.status = " + UserModel.STATUS_ACTIVE;

Now you can use

现在你可以使用

@Query(value=USER_QUERY)

回答by WeGa

I've managed to use class String constant in query via SpEL T() operator, which gives you access to static methods and constants on a given class. For String I have to wrap expression with single quotes ('), probably it will be needed for you as well (if QuerySyntaxException occurs).

我已经设法通过 SpEL T() 运算符在查询中使用类 String 常量,这使您可以访问给定类上的静态方法和常量。对于字符串,我必须用单引号 (') 包装表达式,您可能也需要它(如果发生 QuerySyntaxException)。

Try something like this,

尝试这样的事情,

@Query("SELECT u FROM #{#entityName} u " +
       "WHERE u.status = #{T(fully.qualified.path.UserModel).STATUS_ACTIVE}")

Note: somehow it doesn't work if you use UserModel instead of #{#entityName}.

注意:如果您使用 UserModel 而不是 #{#entityName},则不知何故它不起作用。

In docs its mentioned briefly, see: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-beandef-xml-based

在其简要提及的文档中,请参阅:https: //docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-beandef-xml-based

Don't know since when this is supported, I've got spring-data-jpa 1.4.3, spring-framework 3.2.17

不知道从什么时候开始支持,我有 spring-data-jpa 1.4.3,spring-framework 3.2.17