Jack BeNimble
Jack BeNimble

Reputation: 36663

How to query a Spring Repository with native SQL

I have a simple React/Spring-Boot app that creates and stores licenses. I'm trying to figure out how to incorporate a search function into the app.

The basic concept is the user enters a string and presses a "search" button which will trigger a query to the API in Spring Boot:

license/search/{searchString}

The query will search for the same string in a number of different fields. For example

select * from licenses where company like "'%" + searchString + "%'" or product like "'%" + searchString + "%'" or etc... 

How can this be coded using a repository in Spring boot? I already have the controller repository created, but I need to know how to create the query.

Edit: this is the current repository:

public interface LicenseRepository extends JpaRepository<License, Long>
{
    License findByFullName(String fullName);
    List<License> findAllByUserId(String id);        
}

Upvotes: 1

Views: 146

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521289

Your current attempted query may be too complex for a query method. Instead, consider using the @Query annotation:

@Query("SELECT l FROM License l WHERE l.company LIKE %:company% OR p.product LIKE %:product%")
List<Movie> searchByCompanyLikeOrProductLike(@Param("company") String company, @Param("product") String product);

Upvotes: 2

Related Questions