29

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.

1
  • Have you taken the time to read the reference guide for instance table 4 which explains the keywords... Commented Nov 17, 2016 at 15:52

1 Answer 1

79

You can do that easily in your Interface

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

See the docs for more options

Sign up to request clarification or add additional context in comments.

1 Comment

The link you have provided is very useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.