19
copyright 2013 Trainologic LTD Spring Data and MongoDB

Building Spring Data with MongoDB

  • Upload
    mongodb

  • View
    927

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Building Spring Data with MongoDB

copyright 2013 Trainologic LTD

Spring Data and MongoDBSpring Data and MongoDB

Page 2: Building Spring Data with MongoDB

2copyright 2013 Trainologic LTD

Spring DataSpring 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

2

Page 3: Building Spring Data with MongoDB

3copyright 2013 Trainologic LTD

Spring DataSpring 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

3

Page 4: Building Spring Data with MongoDB

4copyright 2013 Trainologic LTD

Spring DataSpring 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

4

Page 5: Building Spring Data with MongoDB

5copyright 2013 Trainologic LTD

Spring DataSpring 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

5

Page 6: Building Spring Data with MongoDB

6copyright 2013 Trainologic LTD

Spring DataSpring 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

6

Page 7: Building Spring Data with MongoDB

7copyright 2013 Trainologic LTD

Spring DataSpring Data

Example

7

public interface MyCompanyRepository extends MongoRepository<Company,

String>{List<Company> findByCompanyName(String name);

}

Page 8: Building Spring Data with MongoDB

8copyright 2013 Trainologic LTD

Spring DataSpring 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

8

Page 9: Building Spring Data with MongoDB

9copyright 2013 Trainologic LTD

Spring DataSpring 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

9

Page 10: Building Spring Data with MongoDB

10 copyright 2013 Trainologic LTD

Spring DataSpring 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

10

<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/>

Page 11: Building Spring Data with MongoDB

11 copyright 2013 Trainologic LTD

Spring DataSpring 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

11

Page 12: Building Spring Data with MongoDB

12 copyright 2013 Trainologic LTD

Spring DataSpring 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

12

Page 13: Building Spring Data with MongoDB

13 copyright 2013 Trainologic LTD

Spring DataSpring Data

•You can configure your MongoTemplate with a WriteConcernResolver which will allow you to specify a write-concern on a per operation basis.

MongoTemplate

13

Page 14: Building Spring Data with MongoDB

14 copyright 2013 Trainologic LTD

Spring DataSpring 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

14

Page 15: Building Spring Data with MongoDB

15 copyright 2013 Trainologic LTD

Spring DataSpring Data

•The following annotations are provided for mapping:

•@Document, @DBRef, @Indexed, @CompoundIndex, @GeoSpatialIndexed, @Transient, @PersistenceConstructor, @Value, @Field

Supported Domain Mapping

15

Page 16: Building Spring Data with MongoDB

16 copyright 2013 Trainologic LTD

Spring DataSpring Data

findAndModify as an Example

16

•findAndModify receives a query object and an Update object and allows you to perform the famous search&update in MongoDB.

Page 17: Building Spring Data with MongoDB

17 copyright 2013 Trainologic LTD

Spring DataSpring Data

Querying

17

•You can construct a query object using the following ways:

•Use Criteria statements.

•Use the BasicQuery object to provide plain ole’ JSON.

Page 18: Building Spring Data with MongoDB

18 copyright 2013 Trainologic LTD

Spring DataSpring Data

Querying

18

• 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);

Page 19: Building Spring Data with MongoDB

19 copyright 2013 Trainologic LTD

Spring DataSpring Data

Typesafe Queries

19

•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).