sql – Spring Boot JPA Query for not null

我正在使用spring boot JPA,我想只返回status id不为null的值.查询此问题的最佳方法是什么?

@ManyToOne
@JoinColumn(name = "entity_status_id")
private entityStatusLookup entityStatusLookup;

EntityController

public interface EntityRepository extends CrudRepository<Batch, String> {   

    public Page<Entity> findByUploadUserOrderByUploadDateDesc(String userId, Pageable page);

    public Entity findByEntityId(String entityId);

}

API

@RequestMapping(value="/entity/user", method=RequestMethod.GET)
public HttpEntity<PagedResources<Entity>> getEntityByUser(Pageable page, PagedResourcesAssembler assembler) {
    String user = SecurityContextHolder.getContext().getAuthentication().getName();
    Page<Enity> entityItems = entityRepository.findByUploadUserOrderByUploadDateDesc(user, page);

    return new ResponseEntity<>(assembler.toResource(entityItems), HttpStatus.OK);
}

我意识到我可以遍历返回的页面并查找要删除的空值,但我宁愿让查询返回非空值.我不确定在实体状态id上查询非null的最佳方法是什么.

最佳答案
您可以在界面中轻松完成

public interface EntityRepository extends CrudRepository<Batch, String> {   
    Iterable<Entity> findByStatusIdNotNull();
}

有关更多选项,请参见the docs

转载注明原文:sql – Spring Boot JPA Query for not null - 代码日志