In Spring Data Solr Repositories article we will discuss about Spring Data repository, Spring Data project provides Spring Data Repository abstraction to reduce the amount of boilerplate code. Earlier there are various persistence stores require lot of code for data access layers implementation. Spring Data repository abstraction almost avoid all code required to implement data access layers.
Repository is a central interface of the Spring Data repository abstraction. As you can see in the above diagram, the Spring Data project is not only for a particular technologies such as relational databases it is also for NoSQL databases such as MongoDB, Cassandra, Solr etc. But Repository interface is a central interface for all various technologies. It takes the domain class to manage as well as ID type of the domain class as type arguments.
This Repository interface act as a marker interface but Spring Data provides another interface that extend this Repository interface. The name of this interface is CrudRepository, it provides sophisticated CRUD functionality for the entity class that is being managed. Let’s see as following:
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { <S extends T> S save(S entity); Optional<T> findById(ID primaryKey); Iterable<T> findAll(); long count(); void delete(T entity); boolean existsById(ID primaryKey); //... more functionality omitted. }
As you can see the above interface provides several methods, that are required for Data Access layer. As following:
The Spring Data project also provides support for pagination and sorting. The Spring Data repositories has another repository interface that extends CrudRepository to provide pagination and sorting functionalities, that is PagingAndSortingRepository. It is abstraction that adds additional methods to ease paginated access to entities as following:
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> { Iterable<T> findAll(Sort sort); Page<T> findAll(Pageable pageable); }
The above interface adds two more methods, findAll(Sort sort) returns sorted entities and findAll(Pageable pageable) returns results into paginated form. Let’s see how to access the second page of Order by page size of 10 as following:
PagingAndSortingRepository<User, Long> repository = //... get access to a bean Page<Order> orders = repository.findAll(PageRequest.of(1, 10));
As we have seen that how to access paginated results using the PagingAndSortingRepository interface. Let’s see the how to create Spring Data Solr repositories.
Spring Data project also provides persistence technology-specific abstractions, such as SolrCrudRepository, MongoCrudRepository, JpaCrudRepository etc. These interfaces extend CrudRepository and expose the functionalities of the specific persistence technology.
First, define a domain class-specific repository interface. The interface must extend Repository and be typed to the domain class and an ID type. If you want to expose CRUD methods for that domain type, extend CrudRepository instead of Repository.
package com.doj.app.repository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.solr.repository.Query; import org.springframework.data.solr.repository.SolrCrudRepository; import com.doj.app.pojo.Order; /** * @author Dinesh.Rajput * */ public interface SolrOrderRepository extends SolrCrudRepository<Order, Long> { Order findByOrderid(Long orderid); ... }
In the above repository class, Order is a domain class as following:
/** * */ package com.doj.app.pojo; import org.springframework.data.annotation.Id; import org.springframework.data.solr.core.mapping.Indexed; import org.springframework.data.solr.core.mapping.SolrDocument; /** * @author Dinesh.Rajput * */ @SolrDocument(collection="Order") public class Order { @Id @Indexed(name = "oid", type = "long") private Long orderid; @Indexed(name = "oname", type = "string") private String orderName; @Indexed(name = "odesc", type = "string") private String orderDescription; @Indexed(name = "pname", type = "string") private String productName; @Indexed(name = "cname", type = "string") private String customerName; @Indexed(name = "cmobile", type = "string") private String customerMobile; public Long getOrderid() { return orderid; } public void setOrderid(Long orderid) { this.orderid = orderid; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getCustomerMobile() { return customerMobile; } public void setCustomerMobile(String customerMobile) { this.customerMobile = customerMobile; } public String getOrderDescription() { return orderDescription; } public void setOrderDescription(String orderDescription) { this.orderDescription = orderDescription; } }
The above class is domain class for Spring Data Solr repository. Let’s see in the next section how to configure Solr repository using Spring Namespace.
The Spring Data Solr module contains a custom namespace.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:solr="http://www.springframework.org/schema/data/solr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/solr http://www.springframework.org/schema/data/solr/spring-solr.xsd"> <solr:repositories base-package="com.dineshonjava.repositories" /> <solr:solr-client id="solrClient" url="http://locahost:8983/solr" /> </beans>
Using the solr-client element registers an instance of SolrClient in the context. Let’s configure same above configuration using the Annotation based configuration.
The Spring Data Solr repositories support cannot only be activated through an XML namespace but also using an annotation through JavaConfig.
@Configuration @EnableSolrRepositories class ApplicationConfig { @Bean public SolrClient solrClient() { EmbeddedSolrServerFactory factory = new EmbeddedSolrServerFactory("classpath:com/dineshonjava/solr"); return factory.getSolrServer(); } @Bean public SolrOperations solrTemplate() { return new SolrTemplate(solrClient()); } }
In the above configuration, we have enabled Spring Data Solr repository using the @EnableSolrRepositories annotation, which essentially carries the same attributes as the XML namespace does. We can also give basePackage attribute to scan all available repositories.
Let’s see how to create Query methods in Solr Repository in the next section.
The Solr module supports defining a query manually as String or have it being derived from the method name. So deriving the query from the method name is not always sufficient and/or may result in unreadable method names. We can also use the @Query annotation or Solr named queries.
public interface SolrOrderRepository extends SolrCrudRepository<Order, Long> { List findByOrderidAndOrderName(Long orderid, String orderName); }
The method name above will be translated into the following solr query
q=oid:?0 AND oname:?1
You can also bind your own query to query method of the Spring Data Solr repositories using @Query annotation as following:
public interface SolrOrderRepository extends SolrCrudRepository<Order, Long> { ... @Query("odesc:*?0*") Page<Order> findByOrderDescription(String searchTerm, Pageable pageable); @Query("odesc:*?0* OR oname:*?0* OR pname:*?0*") Page<Order> findByCustomerQuery(String searchTerm, Pageable pageable); }
You can also use named queries by declring into properties file in the classpath and load this the properties file at the time configuration for the Spring Data Solr repositories.
Declare named query in properties file
Order.findByNamedQuery=odesc:?0 Order.findByOrderName=oname:?0 public interface SolrOrderRepository extends SolrCrudRepository<Order, Long> { List<Order, Long> findByNamedQuery(String orderDesc); @Query(name = "Order.findByOrderName") List<Order, Long> findByAnnotatedNamedQuery(String name); }
According Spring Document for the Spring Data Solr, following query methods are supported.
Keyword | Sample | Solr Query String |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thanks for reading with the Dineshonjava. Let’s we have lot of articles in the series of the Spring Data Solr tutorial.
Happy learning with us!!! 🙂
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…