Queries

Query Methods

Any implementation of Spring's Repository can define custom queries through method names.

For example, this will fetch all the entity with a specific name:

public interface EntityRepository extends JpaRepository<Entity, EntityKey> {

   public Iterable<Entity> findByName(final String name);

}

JPQL

It is possible to define the query which a method will use:

@Query("SELECT e FROM Entity e WHERE e.name = :name")
public Iterable<Entity> findByNameJpql(@Param("name") final String name);

Notice that it is using a named argument.

SpEL in Queries

To find out automatically the received entity name (useful when extending the entity):

@Query("SELECT e FROM #{#entityName} e WHERE e.name = :name")
public Iterable<Entity> findByNameJpql(@Param("name") final String name);

Query by Example

Example<Employee> example;
Iterable<Employee> employees;

example = Example.of(emp);

employees = repository.findAll(example);

More Information

Last updated