Hot questions for Spring Data Commons

Top 10 Java Open Source / Spring / Spring Data Commons

Question:

I'm working the Spring Data Commons v2+ snapshot, and I see that the constructors for a PageRequest have been deprecated. This appears to have occurred between M1 & M2. Unfortunately, this is the only [real] implementation of the Pageable interface. I'm wondering where the effort is heading, and what a better alternative would be for current development.


Answer:

It's just the constructors which have been deprecated. Instead of

new PageRequest(firstResult, maxResults, new Sort(...))

you can now use

PageRequest.of(firstResult, maxResults, Sort.by(...))

and that's it.

Question:

I'm trying to use spring PagingAndSortingRepository with a find MyEntity where field in fieldValues query as follows:

@Repository
public interface MyEntity extends PagingAndSortingRepository<MyEntity, String> {

    List<MyEntity> findByMyField(Set<String> myField);

}

But of no success.

I expected the above function to return all entities whose field matches one of the field values but it only returns empty results.

Even though it seems like a pretty straight forward ability i could not find any reference to it in the docs.

Is / How that could be achieved?

Thanks.


Answer:

This should indeed be possible if you are searching on a specific field within your entity and you want to return a list of all that field matches at least one entry in some collection. The documentation here says this can be achieved using the keyword In example: findByAgeIn(Collection<Age> ages) and is equivalent to … where x.age in ?1

From your post i'm not 100% sure if this is the use case you are after but give this a try. You will need to search on a specific field, so replace 'field' with whatever field you are searching on. If you are searching on multiple fields it may be possible to concatenate the results with the Or keyword and specify multiple fields that way.

@Repository
public interface MyEntity extends PagingAndSortingRepository<MyEntity, String> {

    List<MyEntity> findByFieldIn(Set<String> myField);

} 

Question:

What is the difference between getSize() and getNumberOfElements in the Spring Data class org.springframework.data.domain.Slice?

The Javadoc does not offer too much help here.


Answer:

getSize() returns the capacity of the Slice.

getNumberOfElements() how many elements does the Slice contains.

For example: You want Page of data from PagingAndSortingRepository. You can call method like repo.findAll(new PageRequest(0,30)) what means you request for first page of data which contains 30 entities at most. Assuming that there are only 10 entities in database you receive a Page where size is 30 and numberOfElements is 10.

Question:

I'm using spring-data-common's PagedResourcesAssembler in my REST controller, and I was happy to see it even generates next/previous links in the response. However, in those cases where I have additional query parameters (besides page, size, sort), these are not included in the generated links. Can I somehow configure the assembler to include the parameters in the links?

Many thanks, Daniel


Answer:

You need to build base link by yourself and pass it to "toResource" method of PagedResourcesAssembler.

@Controller
@RequestMapping(value = "/offer")
public class OfferController {

    private final OfferService offerService;

    private final OfferAssembler offerAssembler;

    @Autowired
    public OfferController(final OfferService offerService, OfferAssembler offerAssembler) {

        this.offerService= checkNotNull(offerService);
        this.offerAssembler= checkNotNull(offerAssembler);
    }

    @RequestMapping(value = "/search/findById", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<PagedResources<OfferResource>> findOfferById(
            @RequestParam(value = "offerId") long offerId, Pageable pageable,
            PagedResourcesAssembler<OfferDetails> pagedResourcesAssembler) {
        Page<OfferDetails> page = service.findById(offerId, pageable);

        Link link = linkTo(methodOn(OfferController.class).findOfferById(offerId,
                pageable,
                pagedResourcesAssembler)).withSelfRel();
        PagedResources<OfferResource> resource = pagedResourcesAssembler.toResource(page, assembler, link);
        return new ResponseEntity<>(resource, HttpStatus.OK);
    }
}

As a result you will get:

http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]{&page,size,sort}