Александр Третьяков: "spring data jpa and mongodb"

Post on 11-May-2015

175 Views

Category:

Documents

8 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Evolution of persistence

Spring Data

Alexandr Tretyakov

Dnipropetrovs’k

31 July 2014

2 April 12, 2023

Agenda The NoSQL landscape Spring Data JPA MongoDB in a nutshell Spring Data MongoDB

April 12, 2023 3

The NoSQL landscape

4 April 12, 2023

Variety of databases

April 12, 2023 5

Spring Data JPA

6 April 12, 2023

Project goals Spring Data provides a familiar and consistent

Spring-based programming model for NoSQL and relational stores while retaining store-specific features and capabilities.

7 April 12, 2023

Spring Data sub-projects Spring Data JPA Spring Data MongoDB Spring Data Neo4J Spring Data Redis Spring for Apache Hadoop Spring Data Gemfire Spring Data REST Spring Data JDBC Extensions

8 April 12, 2023

Don't repeat the DAOpublic interface GenericDao <T, PK extends Serializable> {

/* Persist the newInstance object into database */

PK create(T newInstance);

/** Retrieve an object that was previously persisted to the database using the indicated id as primary key */ T read(PK id);

/* Save changes made to a persistent object */

void update(T transientObject);

/** Remove an object from persistent storage in the database */

void delete(T persistentObject);}

9 April 12, 2023

Don't repeat the DAOpublic class GenericDaoHibernateImpl <T, PK extends Serializable>

implements GenericDao <T, PK> {

private Class<T> type;

public GenericDaoHibernateImpl(Class<T> type) { this.type = type; }

public PK create(T o) { return (PK) getSession().save(o); }

public T read(PK id) { return (T) getSession().get(type, id); } // …

}

10 April 12, 2023

Don't repeat the DAO

public interface PersonDao extends GenericDao<Person, Long> {

List<Person> findByName(String name);

}

public class PersonDaoImpl implements PersonDao {

//implementation

}

The concept of a single generic typesafe DAO had been a topic since the appearance of generics in the Java language.

11 April 12, 2023

Spring Data managed DAORepository interface abstraction has predefined set of

methods for basic functionality.

Repository

– A plain marker interface to let the Spring Data infrastructure pick up user-defined repositories

CrudRepository

– Extends Repository and adds basic persistence methods like saving, finding, and deleting entities

PagingAndSortingRepository

– Extends CrudRepository and adds methods for accessing entities page by page and sorting them by given criteria

12 April 12, 2023

Spring Data managed DAO@NoRepositoryBean

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {

<S extends T> S save(S entity);

T findOne(ID id);

Iterable<T> findAll();

long count();

boolean exists(ID id);

// … more functionality omitted

}

13 April 12, 2023

Spring Data JPA features Sophisticated support to build repositories based on

Spring and JPA Support for Querydsl predicates and thus type-safe

JPA queries Pagination support, dynamic query execution, ability

to integrate custom data access code Validation of @Query annotated queries at bootstrap

time Support for XML based entity mapping JavaConfig based repository configuration by

introducing @EnableJpaRepositories.

14 April 12, 2023

Configuration (XML)<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/data/jpa

http://www.springframework.org/schema/data/jpa/spring-jpa.xsd”>

<jpa:repositories base-package="com.itretiy.springdata.repository.jpa" repository-impl-postfix=”Impl” >

<context:include-filter type=”regex” expression=”.*Repository” />

</jpa:repositories>

</beans>

15 April 12, 2023

Configuration Create an interface either extending Repository or annotated with

@RepositoryDefinition Add the methods you want to provide (make sure that it complies with

query derivation mechanism)

Mostly that’s it what you need for start! (And of course do not forget to set up JPA EntityManager or SessionFactory if you are working directly with Hibernate).

16 April 12, 2023

Querying and fine-tuning the queries Query creation from method names

– findBy* Using JPA NamedQueries

– @NamedQuery and @NamedNativeQuery Using @Query

– @Query(JPQL)

– @Query(SQL)

– @Query with @Param and @Modifying Specifications for Criteria and Predicate Querydsl predicates

17 April 12, 2023

Custom method implementations Add interface for custom repository functionality

interface CustomRepository {

public void customMethod(DomainClass domainClass);

}

Add implementation of custom repository functionalityclass CustomRepositoryImpl implements CustomRepository {

public void customMethod(DomainClass domainClass) { // Your custom implementation}

}

Change your basic repository interfacepublic interface BasicUserRepository extends

CrudRepository<DomainClass, IdClass>, CustomRepository { // Declare other query methods here

}

18 April 12, 2023

Fine-tuning repository definition Create an interface either extending Repository or annotated with @Repository

Definition and annotate it with @NoRepositoryBean

Add the methods you want to expose to it and make sure they actually match the signatures of methods provided by the Spring Data base repository interfaces@NoRepositoryBean

interface MyBaseRepository<T,ID extends Serializable> extends Repository<T, ID> { T findOne(ID id); T save(T entity);}

Use this interface as a base interface for the interface declarations for your entities

interface UserRepository extends MyBaseRepository<User, Long> { User findByEmailAddress(EmailAddress emailAddress);}

19 April 12, 2023

QueryDSL integration Set up maven-apt-plugin with appropriate processor

– QuerydslAnnotationProcessor

– JPAAnnotationProcessor

– HibernateAnnotationProcessor

– JDOAnnotationProcessor

– MongoAnnotationProcessor

Extend QueryDslPredicateExecutor in your repository interface

20 April 12, 2023

Spring Data JPA weaknesses Less of freedom, less flexibility and more conventions

(arguably) Оne more layer, hiding details (arguably)

April 12, 2023 21

MongoDB in a nutshell

22 April 12, 2023

Terminology

RDBMS MongoDB

Database Database

Table Collection

Row(s) JSON Document

Row(s) Field

Index Index

Join Embedded documents & linking

23 April 12, 2023

Document data model Document data model

{ _id: ObjectID('4bd9e8e17cefd644108961bb'),

name: “The Godfather”,

details: { isbn: “0451217403”,

author: “Mario Puzo”

},

price: 100.00,

category: [ new ObjectID('4bf9bec50e32f82523389314')],

comments: [ { user: “Don Corleone”,

text: “Perfect!”,

}

]

}

Schemaless

{ _id: ObjectID('4bf9bec50e32f82523389315'),

name: “Lvivske”,

price: 5.50,

category: [ new ObjectID('4bf9bec50e32f82523389316')]

}

24 April 12, 2023

Key features Rich queries

– If you’re coming from a relational database system where ad hoc queries are the norm, then it is sufficient to note that MongoDB features a similar level of queryability

Secondary indexes

– Secondary indexes in MongoDB are implemented as B-trees. The kinds of indexes supported include all the ones you would find in an RDMBS; ascending, descending, unique, compound-key, and even geospatial

Lack of multi-document transactions No RDBMS like join support

– Two ways for relating documents: manual references and DBRefs

25 April 12, 2023

Replication Replica set

26 April 12, 2023

Scaling Auto-sharding

27 April 12, 2023

MongoDB Java DriverMongo mongo = new MongoClient("localhost", 27017);

mongo.setWriteConcern(WriteConcern.SAFE);

DB database = mongo.getDB("database");

DBCollection collection = database.getCollection("collection");

DBObject data = new BasicDBObject();

data.put("key", "value");

collection.insert(data);

April 12, 2023 28

Spring Data MongoDB

29 April 12, 2023

Spring Data MongoDB feautures MongoTemplate

– take care of acquiring a connection through the configured MongoDbFactory and clean it up properly after the interaction with the store has ended or an exception occurred

– Translate Mongo specific exception into DataAccessException hierarchy

– Provide general-purpose, high-level methods that enable you to execute commonly needed operations as one-line statements (like findOne, findAll, etc.)

– Provide low-level, callback-driven methods that allow you to interact with the MongoDB driver API (starts with execute*)

Object mapping

– Annotation based: @Document, @Field, @Index Repository support

– Queries are derived from method signatures

– Geospatial queries Cross-store support (worth saying it is pretty poor)

30 April 12, 2023

Spring data mapping Abstraction MongoMappingContext

– responsible for building up the domain class metamodel to avoid reflection lookups (e.g., to detect the id property or determine the field key on each and every persistence operation)

Abstraction MappingMongoConverter

– performing the conversion using the mapping information provided by the MongoMappingContext

@Document annotation optional annotation that helps mapping subsystem convert domain objects to the Mongo specific DBObjects and vice versa

@Index for single properties and @CompoundIndex for multiple properties indexes

Do not need annotate relationship between parent and nested documents @DBRef if you want denormalized model

– support lazy loading

– assume that related documents stored in the same database, because mongodb java driver does not support $db property in DBRefs

– lack of support of cascading save Converter interface abstraction for implementing custom converters

31 April 12, 2023

Configuration (XML)<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mongo="http://www.springframework.org/schema/data/mongo"

xsi:schemaLocation="http://www.springframework.org/schema/data/mongo

http://www.springframework.org/schema/data/mongo/spring-mongo.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<mongo:db-factory id="mongoDbFactory" dbname="test" />

<mongo:mapping-converter id="mongoConverter"

base-package="com.itretiy.springdata.domain"

db-factory-ref=”mongoDbFactory”/>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

<constructor-arg ref="mongoDbFactory" />

<property name="writeConcern" value="SAFE" />

</bean>

</beans>

32 April 12, 2023

Spring Data MongoDB repositories Spring Data MongoDB repositories and query

derivation mechanism works as well as for JPA module (Obviously except for @Query syntax)

33 April 12, 2023

MongoDB JSON based query Placeholders ?* lets you substitute the value from the

method arguments into the JSON query stringpublic interface PersonRepository extends MongoRepository <Person, String> @Query("{ 'firstname' : ?0 }") List<Person> findByThePersonsFirstname(String firstname);}

Use the filter property to restrict the set of properties that will be mapped into the Java objectpublic interface PersonRepository extends MongoRepository <Person, String> @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") List<Person> findByThePersonsFirstname(String firstname);}

34 April 12, 2023

Spring Data MongoDB weaknesses Lack of cascade saving of related documents Java driver do not support db in DBRef so related

document must be saved in the same database (arguably)

Poor cross-store support

Your QR Code

I am at your disposal in case of any questions or

doubts

31 July 2014

Alexandr Tretyakov

Dnipropetrovs’k

itretiy@gmail.com

top related