简体   繁体   English

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

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

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 查询中的第二个示例中指定常量?

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.但它的坏处是 IDE 不会将此识别为 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 .这已在IntelliJ IDEA 2017.1 中解决。 I don't know about other IDEs.我不知道其他 IDE。

I would recommend creating an Enum and 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);
   }

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)

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.我已经设法通过 SpEL T() 运算符在查询中使用类 String 常量,这使您可以访问给定类上的静态方法和常量。 For String I have to wrap expression with single quotes ('), probably it will be needed for you as well (if QuerySyntaxException occurs).对于字符串,我必须用单引号 (') 包装表达式,您可能也需要它(如果发生 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

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 实现可能有自己的解决方案,但一个人的休眠似乎不支持,也不支持此处其他答案建议的任何方法。

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

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

Yes, this is a more elegant way and it is readable too.是的,这是一种更优雅的方式,它也具有可读性。 For example:例如:

@Query("SELECT xyz.id FROM XYZ AS xyz WHERE xyz.status = " + Status.OPEN + " AND xyz.active=1")

Here, the Status is a simple class containing static constants:这里, Status是一个包含静态常量的简单类:

public class Status {
    public static final int OPEN = 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM