Example 1. PersonRepository
The repository extends JpaRepository
and passes the JPA entity and it's primary key being managed.
Basic methods for finding a single record, all records, paginated records, create/update, and delete
are automatically provided. It's also very easy to overload any custom query to add pagination and sorting.
The findByFirstNameLike
method let's Spring Data JPA
automatically generate a like query for the first name column, and findByLastName
for an equals query for the last name column. The third method, findByAddress
, creates a
custom query using @Query
and a standard JQL query. @Param
is used before the method param
to create a named parameter. Otherwise it would have created a position based param (ex: 'a.address = ?0').
Note | |
---|---|
Refer to Spring Data JPA's query creation section to see all the different ways the method name can be overloaded to automatically generate queries (And/Or/Between/LessThan/GreaterThan/etc.). |
public interface PersonRepository extends JpaRepository<Person, Integer> { public final static String FIND_BY_ADDRESS_QUERY = "SELECT p " + "FROM Person p LEFT JOIN p.addresses a " + "WHERE a.address = :address"; /** * Find persons like first name. */ public List<Person> findByFirstNameLike(String firstName); /** * Find persons by last name. */ public List<Person> findByLastName(String lastName); /** * Find persons by address. */ @Query(FIND_BY_ADDRESS_QUERY) public List<Person> findByAddress(@Param("address") String address); ... }
Example 2. PersonRepository
Page Query
The second findByAddress
query performs the same search as the first one,
but a Pageable
parameter has been added to the method. The page, and
number of records for a page can be passed in.
Note | |
---|---|
Besides being able to add |
public interface PersonRepository extends JpaRepository<Person, Integer> { ... /** * Find paged persons by address. */ @Query(FIND_BY_ADDRESS_QUERY) public Page<Person> findByAddress(@Param("address") String address, Pageable page); ... }
Example 3. PersonRepository
Named Query
The final query, findByName
, searches for first and last name using a custom query. But instead of
embedding the query in Java it is named query in the orm.xml. This may make it easier
to manage larger queries.
Note | |
---|---|
The query is automatically matched to '${domainClassName}.${methodName}',
but this can be overridden using the |
<?xml version="1.0" encoding="UTF-8"?> <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd" version="2.0"> <named-query name="Person.findByName"> <query>select p from Person p where p.firstName = :firstName AND p.lastName = :lastName</query> </named-query> </entity-mappings>
public interface PersonRepository extends JpaRepository<Person, Integer> { ... /** * Find persons by first and last name. */ public List<Person> findByName(@Param("firstName") String firstName, @Param("lastName") String lastName); }