Bhe*_*zie 10 sql spring spring-data-jpa spring-boot
我正在使用spring boot JPA,我想只返回status id不为null的值.查询此问题的最佳方法是什么?
域
@ManyToOne
@JoinColumn(name = "entity_status_id")
private entityStatusLookup entityStatusLookup;
Run Code Online (Sandbox Code Playgroud)
EntityController
public interface EntityRepository extends CrudRepository<Batch, String> {
public Page<Entity> findByUploadUserOrderByUploadDateDesc(String userId, Pageable page);
public Entity findByEntityId(String entityId);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
我意识到我可以遍历返回的页面并查找要删除的空值,但我宁愿让查询返回非空值.我不确定在实体状态ID上查询非null的最佳方法是什么.
chr*_*con 30
您可以在界面中轻松完成
public interface EntityRepository extends CrudRepository<Batch, String> {
Iterable<Entity> findByStatusIdNotNull();
}
Run Code Online (Sandbox Code Playgroud)
有关更多选项,请参阅文档
| 归档时间: |
|
| 查看次数: |
20878 次 |
| 最近记录: |