Bhetzie
Bhetzie

Reputation: 2922

Spring Boot JPA Query for not null

I'm using spring boot JPAs and I want to only return values where the status id is not null. What's the best way to query for this?

Domain

@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);
}

I realize that I could loop through the returned pages and look for nulls to remove, but I'd rather have the query just return values that are not null. I'm not sure what the best way to query for not null on the entity status id.

Upvotes: 29

Views: 80313

Answers (1)

baao
baao

Reputation: 73211

You can do that easily in your Interface

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

See the docs for more options

Upvotes: 79

Related Questions