Uploaded image for project: 'Spring Data REST'
  1. Spring Data REST
  2. DATAREST-502

Not convenient to search a repository by a related entity using links

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.2.2 (Evans SR2)
    • Fix Version/s: 2.4 M1 (Gosling)
    • Component/s: None
    • Labels:
      None
    • Sprint:
      57 - Fowler Aftermath

      Description

      It doesn't seem to be very convenient to search a repository using an related entity as a query parameter. Consider the following repository:

      @RepositoryRestResource
      public interface TagRepository extends PagingAndSortingRepository<Tag, Long> {
      
          Page<Tag> findAllByNameContainingIgnoreCaseAndOrganisationOrderByNameAsc(@Param("name") String name,
                                                                                   @Param("organisation") Organisation organisation,
                                                                                   Pageable pageable);
      }
      

      Organisations have many tags, a tag has one organisation. The HATEOAS semantics return entity relationships as links, however we cannot pass one of these links as the "organisation" query parameter above.

      As far as I can tell, the only way to pass an organisation parameter is by using the ID (primary key) of the entity directly. This is quite inconvenient as it means we need to resolve links on the client first in order to determine the ID we need and also means that we need to expose the IDs in our DTOs (which is not default and seems like a bad idea), or attempt to parse it from the URI link.

      This is how I would expect it to be able to work:

      http://localhost:8080/api/tags/search/findAllByNameContainingIgnoreCaseAndOrganisationOrderByNameAsc?name=tagName&organisation=http://localhost:8080/api/organisations/1

      However we are currently required to use it like this:

      http://localhost:8080/api/tags/search/findAllByNameContainingIgnoreCaseAndOrganisationOrderByNameAsc?name=tagName&organisation=1

      Is this the only way to query using a related entity?

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              olivergierke Oliver Drotbohm
              Reporter:
              jdmunro James Munro
              Last updater:
              Alexander
              Votes:
              1 Vote for this issue
              Watchers:
              8 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: