简体   繁体   中英

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:

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

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.

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);
   }

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

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.

当您想直接在 @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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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