Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-292

Support query internal LIKE expressions

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.3.1, 1.4 M1
    • Component/s: Core
    • Labels:
      None

      Description

      Let us consider the following method:

      	@Query("SELECT owner FROM Owner owner WHERE owner.lastName LIKE :lastName%")
      	Collection<Owner> findByLastName(String lastName) throws DataAccessException;
      	

      The above code is not supported because, according to JPA, we should have something like this:

      	entityManager.createQuery("SELECT owner FROM Owner owner WHERE owner.lastName LIKE :lastName")
      	query.setParam("lastName", lastName + "%")
      	

      So the current suggested way in Spring Data is to rely on query generation using the 'StartingWith' suffix:

      	Collection<Owner> findByLastNameStartingWith(String lastName) throws DataAccessException;
      	

      It works, except when we'd like to use a fetch join.
      It would be great to have the possibility to do this:

      	@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
      	Collection<Owner> findByLastName(String lastName) throws DataAccessException;
      	

      I think the best way to handle it is to add some preprocessing code so Spring Data would first search for "%" characters and add them to the parameter's value (if any).

        Attachments

          Activity

            People

            • Assignee:
              olivergierke Oliver Gierke
              Reporter:
              michael.isvy Michael Isvy
              Last updater:
              Trevor Marshall
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: