春季启动JPA查询不为空
问题描述:
我使用弹簧启动JPA,我只想返回状态ID不为null的值。什么是查询这个最好的方法?春季启动JPA查询不为空
域
@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
你花时间阅读[参考指南](http://docs.spring.io/ spring-data/jpa/docs/current/reference/html /#jpa.query-methods.query-creation)例子* table 4 *解释关键字... –