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

自闭症网瘾萝莉.ら 提交于 2019-12-03 14:43:03

问题


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:

@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?


回答1:


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.




回答2:


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

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:

   @Autowired
   UserModelRepository userModelRepository;

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



回答3:


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)



回答4:


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

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}.

In docs its mentioned briefly, see: 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




回答5:


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.




回答6:


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

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


来源:https://stackoverflow.com/questions/18402988/is-there-a-way-to-use-constants-inside-spring-data-query-annotation-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!