Spring data jpa 在jpql的where中使用枚举

spring data jpa中,内置了需要方便而实用的实现,写一个接口,继承JpaRepository<T,ID> 。这样你的这个接口就会有个实现类SimpleJpaRepository。它内置了很多方法,实现的接口:JpaRepository,JpaSpecificationExecutor,PagingAndSortingRepository,QueryByExampleExecutor,CrudRepository。哎,功能之强大,不多说,有时候在查询指定条件的实体时,我们希望写jpql来实现。例如:

@Query("select p from WalletPay p where id=:id")
WalletPay lockById(@Param("id") Long id);

枚举作为where条件时,一样的在参数中传入接口。而有些时候,我们需要where中指定一些枚举作为where条件,这个时候的jqpl写法有些变化。

/**
 * 查询可以做的task
 * @param lastTime 最后修改时间
 * @param now 当前时间
 * @return
 */
@Query("select t from NotifyTask t where " + "(t.state=cn.lowang.task.domain.enums.TaskState.NONE and nextTime<=?2) "
        + "or (t.state=cn.lowang.task.domain.enums.TaskState.FAIL and nextTime<=?2) "
        + "or (t.state=cn.lowang.task.domain.enums.TaskState.DOING and t.lastmodifiedTime<=?1)")
public List<NotifyTask> listValidTask(Date lastTime, Date now);

在jpql中,我们需要指明枚举的全路径和枚举值。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容