2016-11-17 138 views
0

我使用彈簧啓動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的最佳方式。

+0

你花時間閱讀[參考指南](http://docs.spring.io/ spring-data/jpa/docs/current/reference/html /#jpa.query-methods.query-creation)例子* table 4 *解釋關鍵字... –

回答

6

你可以做到這一點很容易在你的界面

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

更多選項見the docs