Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding Queries to Spring Data-JPA

DZone's Guide to

Adding Queries to Spring Data-JPA

· Java Zone ·
Free Resource

FlexNet Code Aware, a free scan tool for developers. Scan Java, NuGet, and NPM packages for open source security and open source license compliance issues.

In my previous post, Simpler JPA with Spring Data-JPA I showed how to configure a repository interface which Spring Data will implement for us. Let me show how easy it is to add queries.

Modify the Repository Interface

Simply define the method interface and use the @Query annotation to define the OQL.

package com.gordondickens.myapp.repository;
 
import org.springframework.data.repository.CrudRepository;
import com.gordondickens.myapp.entity.Product;
 
public interface ProductRepository
   extends CrudRepository<Product, Long> {
 
    @Query("FROM Product")
    List<Product> findAllProducts();
}

Modifying Queries

  • Parameters can be marked by position with ?1, ?2, etc.
  • Parameters can be marked by name with :paramName and annotation @Param("paramName")
  • Modifying method signature can only return void, Integer or int
  • Updating queries MUST be transactional, mark with @Transactional
  • Spring Data will drop all non-flushed changes pending in the EntityManager, change with @Modifying(clearAutomatically=false)
package com.gordondickens.myapp.repository;
 
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
 
@Transactional(readOnly=true)
public interface ProductRepository
   extends CrudRepository<Product, Long> {
 
   @Query("FROM Product")
   List<Product> findAllProducts();
 
   // Example with positional params
   @Modifying
   @Transactional(readOnly=false)
   @Query("update Product p set p.description = ?2 where p.productId = ?1")
   Integer setNewDescriptionForProduct(String productId, String description);
 
   // Example with named params
   @Modifying
   @Query("update Product p set p.description = :description where p.productId = :productId")
   Integer setNewDescriptionForProduct(@Param("productId") String productId,
      @Param("description") String description);
}

Automatic Query Generation

The <jpa:repositories/> has an option query-lookup-strategy which defaults to “create-if-not-found” which will generate queries for us.

The default is “create-if-not-found“. Other options are “create” or “use-declared-query“.

<jpa:repositories base-package="com.gordondickens.myapp.repository"
     query-lookup-strategy="create-if-not-found"/>

To create a find method that effectively does @Query("FROM Product p where p.productId = :productId")

public interface ProductRepository extends CrudRepository<Product, Long> {
   ...
 
   @Query
   Product findByProductId(String productId);
 
   ...

Summary

We see how simple interface additions provide custom methods based on query language. We can query either by positional or named parameters.

From http://gordondickens.com/wordpress/2011/08/02/adding-queries-to-spring-data-jpa/

 Scan Java, NuGet, and NPM packages for open source security and license compliance issues. 

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}