2. copyright 2013 Trainologic LTD
Spring Data
• Spring Data provides a set of utilities for working with
NoSQL databases.
• It also provides support for relational databases and
REST interfaces.
• We are going to focus over MongoDB support.
• But first, some basics…
Spring Data
3. copyright 2013 Trainologic LTD
Spring Data
• Spring Data Commons provides a common API for the
different Spring Data subprojects.
• The main interface in Spring Data is the Repository
interface.
• This is a marker interface that is mainly used for
discovering extending classes.
Data Commons
4. copyright 2013 Trainologic LTD
Spring Data
• The main interface that extends from Repository is the
CrudRepository one.
• It provides methods for saving, deleting, counting and
retrieving of your domain class.
• Usually a specific storage technology class will be
available that extends this one (e.g., MongoRepository).
• The PagingAndSortingRepository interface provides
paging and sorting capabilities.
CrudRepository
5. copyright 2013 Trainologic LTD
Spring Data
• If you don’t feel like exposing the entire CrudRepository
interface, you can annotate your class with the
@RepositoryDefinition annotation.
• Then, you can selectively provides the required
methods.
Custom Repositories
6. copyright 2013 Trainologic LTD
Spring Data
• Spring Data provides a mechanism for generating
custom queries for your storage technology.
• A four steps recipe:
1. Define an interface that extends from the
appropriate repository interface.
2. Add your query signatures.
3. Instruct Spring to create proxy for your repository.
4. Inject the generated bean and use it.
Custom Queries
7. copyright 2013 Trainologic LTD
Spring Data
Example
public interface MyCompanyRepository extends MongoRepository<Company,
String>{
List<Company> findByCompanyName(String name);
}
8. copyright 2013 Trainologic LTD
Spring Data
• Spring Data can construct the actual query according to
the method name or by additional declaration.
• If selecting the method name way then:
• The ‘By’ acts as a separator.
• Inherent support for And and Or.
• Support for Between, LessThan, GreaterThan and
Like.
• Support IgnoreCase.
• And OrderBy with additional Asc or Desc.
Query Syntax
9. copyright 2013 Trainologic LTD
Spring Data
• Query methods can also accept additional special
parameters of the following types:
• Sort – allows to configure a sort operation.
• Pageable – allows to configure paging.
Query Syntax
10. copyright 2013 Trainologic LTD
Spring Data
• In order to connect your Spring project to
MongoDB, you need to create a connection.
• Can be done with either annotations or XML namespace
(as usual in Spring).
• XML Schema example:
Connecting to MongoDB
<mongo:mongo replica-set="127.0.0.1:27017,localhost:27018">
<mongo:options connections-per-host="8"
threads-allowed-to-block-for-connection-multiplier="4"
connect-timeout="1000“
auto-connect-retry="true
slave-ok="true
write-fsync="true"/>
</mongo:mongo/>
11. copyright 2013 Trainologic LTD
Spring Data
• MongoDB is a very popular NoSQL Database.
• Spring Data provides many features for easing (the
already easy) programming to MongoDB.
• Two main features:
• MongoTemplate.
• Mongo Repositories.
Spring Data for MongoDB
12. copyright 2013 Trainologic LTD
Spring Data
• As usual in Spring, template objects are both thread-
safe and stateless.
• MongoTemplate provides API resembles as possible the
native MongoDB driver for Java.
• The main difference is that the MongoTemplate can
receive your model objects directly and not only
DBObject.
• Your objects are mapped/converted to MongoDB
objects.
MongoTemplate
13. copyright 2013 Trainologic LTD
Spring Data
• You can configure your MongoTemplate with a
WriteConcernResolver which will allow you to specify a
write-concern on a per operation basis.
MongoTemplate
14. copyright 2013 Trainologic LTD
Spring Data
• Id mapping is handled as follows:
• Usage of the @Id annotation (not the JPA one).
• A property/field named ‘id’.
• If you don’t define one, the framework will generate an
id for you.
Id
15. copyright 2013 Trainologic LTD
Spring Data
• The following annotations are provided for mapping:
• @Document, @DBRef, @Indexed, @CompoundIndex,
@GeoSpatialIndexed, @Transient, @PersistenceCons
tructor, @Value, @Field
Supported Domain Mapping
16. copyright 2013 Trainologic LTD
Spring Data
findAndModify as an Example
• findAndModify receives a query object and an Update
object and allows you to perform the famous
search&update in MongoDB.
17. copyright 2013 Trainologic LTD
Spring Data
Querying
• You can construct a query object using the following
ways:
• Use Criteria statements.
• Use the BasicQuery object to provide plain ole’ JSON.
18. copyright 2013 Trainologic LTD
Spring Data
Querying
• If you want to customize the query in a JSON way, you
can use the @Query annotation.
• E.g.:
@Query(value="{ ‘lastName' : ?0 }", fields="{ 'firstName' : 1, 'lastName' : 1}")
List<Student> findStudentsByLastName(String lastName);
19. copyright 2013 Trainologic LTD
Spring Data
Typesafe Queries
• You can also perform typesafe queries with the Query
DSL.
• However, in order to use it, you should generate DSL
code using APT (resembles typed criteria model from
JPA 2.0).