【问题标题】:Spring Boot JPA Query for not nullSpring Boot JPA 查询不为空
【发布时间】:2017-04-01 06:02:17
【问题描述】:

我正在使用 Spring Boot JPA,我只想返回状态 ID 不为空的值。查询此问题的最佳方法是什么?

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

实体控制器

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 上查询 not null 的最佳方法是什么。

【问题讨论】:

标签: sql spring spring-boot spring-data-jpa


【解决方案1】:

您可以在界面中轻松做到这一点

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

更多选项请见the docs

【讨论】:

  • 你提供的链接很有用。
猜你喜欢
  • 2021-10-13
  • 2021-09-15
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 2020-03-29
  • 1970-01-01
  • 2019-07-16
相关资源
最近更新 更多