SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Evolution of persistence
Spring Data
Alexandr Tretyakov
Dnipropetrovs’k
31 July 2014
2
Agenda
 The NoSQL landscape
 Spring Data JPA
 MongoDB in a nutshell
 Spring Data MongoDB
4 August 2014
34 August 2014
The NoSQL
landscape
4
Variety of databases
4 August 2014
54 August 2014
Spring Data JPA
6
Project goals
4 August 2014
 Spring Data provides a familiar and consistent Spring-based
programming model for NoSQL and relational stores while
retaining store-specific features and capabilities.
7
Spring Data sub-projects
4 August 2014
 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
Don't repeat the DAO
4 August 2014
public 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
Don't repeat the DAO
4 August 2014
public 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
Don't repeat the DAO
4 August 2014
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
Spring Data managed DAO
4 August 2014
Repository 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
Spring Data managed DAO
4 August 2014
@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
Spring Data JPA features
4 August 2014
 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
Configuration (XML)
4 August 2014
<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
Configuration
4 August 2014
 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
Querying and fine-tuning the queries
4 August 2014
 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
Custom method implementations
4 August 2014
 Add interface for custom repository functionality
interface CustomRepository {
public void customMethod(DomainClass domainClass);
}
 Add implementation of custom repository functionality
class CustomRepositoryImpl implements CustomRepository {
public void customMethod(DomainClass domainClass) {
// Your custom implementation
}
}
 Change your basic repository interface
public interface BasicUserRepository extends
CrudRepository<DomainClass, IdClass>, CustomRepository {
// Declare other query methods here
}
18
Fine-tuning repository definition
4 August 2014
 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
QueryDSL integration
4 August 2014
 Set up maven-apt-plugin with appropriate processor
– QuerydslAnnotationProcessor
– JPAAnnotationProcessor
– HibernateAnnotationProcessor
– JDOAnnotationProcessor
– MongoAnnotationProcessor
 Extend QueryDslPredicateExecutor in your repository interface
20
Spring Data JPA weaknesses
4 August 2014
 Less of freedom, less flexibility and more conventions
(arguably)
 Оne more layer, hiding details (arguably)
214 August 2014
MongoDB in a
nutshell
22
Terminology
4 August 2014
RDBMS MongoDB
Database Database
Table Collection
Row(s) JSON Document
Row(s) Field
Index Index
Join Embedded documents & linking
23
Document data model
4 August 2014
 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
Key features
4 August 2014
 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
Replication
4 August 2014
 Replica set
26
Scaling
4 August 2014
 Auto-sharding
27
MongoDB Java Driver
4 August 2014
Mongo 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);
284 August 2014
Spring Data
MongoDB
29
Spring Data MongoDB feautures
4 August 2014
 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
Spring data mapping
4 August 2014
 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
Configuration (XML)
4 August 2014
<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
Spring Data MongoDB repositories
4 August 2014
 Spring Data MongoDB repositories and query derivation
mechanism works as well as for JPA module (Obviously
except for @Query syntax)
33
MongoDB JSON based query
4 August 2014
 Placeholders ?* lets you substitute the value from the
method arguments into the JSON query string
public 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 object
public interface PersonRepository extends MongoRepository <Person, String>
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
List<Person> findByThePersonsFirstname(String firstname);
}
34
Spring Data MongoDB weaknesses
4 August 2014
 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

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Lucene & Solr and Usecases
Introduction to Lucene & Solr and UsecasesIntroduction to Lucene & Solr and Usecases
Introduction to Lucene & Solr and UsecasesRahul Jain
 
Building Spring Data with MongoDB
Building Spring Data with MongoDBBuilding Spring Data with MongoDB
Building Spring Data with MongoDBMongoDB
 
Content extraction with apache tika
Content extraction with apache tikaContent extraction with apache tika
Content extraction with apache tikaJukka Zitting
 
High Performance JSON Search and Relational Faceted Browsing with Lucene
High Performance JSON Search and Relational Faceted Browsing with LuceneHigh Performance JSON Search and Relational Faceted Browsing with Lucene
High Performance JSON Search and Relational Faceted Browsing with Lucenelucenerevolution
 
Tutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginTutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginsearchbox-com
 
MongoDB 2.4 and spring data
MongoDB 2.4 and spring dataMongoDB 2.4 and spring data
MongoDB 2.4 and spring dataJimmy Ray
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with SolrErik Hatcher
 
SharePoint TechCon 2009 - 801
SharePoint TechCon 2009 - 801SharePoint TechCon 2009 - 801
SharePoint TechCon 2009 - 801Andreas Grabner
 
Introduction to SharePoint 2013 REST API
Introduction to SharePoint 2013 REST APIIntroduction to SharePoint 2013 REST API
Introduction to SharePoint 2013 REST APIQUONTRASOLUTIONS
 
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...Edureka!
 
Munching & crunching - Lucene index post-processing
Munching & crunching - Lucene index post-processingMunching & crunching - Lucene index post-processing
Munching & crunching - Lucene index post-processingabial
 
Battle of the giants: Apache Solr vs ElasticSearch
Battle of the giants: Apache Solr vs ElasticSearchBattle of the giants: Apache Solr vs ElasticSearch
Battle of the giants: Apache Solr vs ElasticSearchRafał Kuć
 
Apache Lucene intro - Breizhcamp 2015
Apache Lucene intro - Breizhcamp 2015Apache Lucene intro - Breizhcamp 2015
Apache Lucene intro - Breizhcamp 2015Adrien Grand
 
Faceted Search with Lucene
Faceted Search with LuceneFaceted Search with Lucene
Faceted Search with Lucenelucenerevolution
 
Intro to Elasticsearch
Intro to ElasticsearchIntro to Elasticsearch
Intro to ElasticsearchClifford James
 

Was ist angesagt? (20)

Introduction to Lucene & Solr and Usecases
Introduction to Lucene & Solr and UsecasesIntroduction to Lucene & Solr and Usecases
Introduction to Lucene & Solr and Usecases
 
Building Spring Data with MongoDB
Building Spring Data with MongoDBBuilding Spring Data with MongoDB
Building Spring Data with MongoDB
 
Content extraction with apache tika
Content extraction with apache tikaContent extraction with apache tika
Content extraction with apache tika
 
L12: REST Service
L12: REST ServiceL12: REST Service
L12: REST Service
 
High Performance JSON Search and Relational Faceted Browsing with Lucene
High Performance JSON Search and Relational Faceted Browsing with LuceneHigh Performance JSON Search and Relational Faceted Browsing with Lucene
High Performance JSON Search and Relational Faceted Browsing with Lucene
 
Thinking restfully
Thinking restfullyThinking restfully
Thinking restfully
 
Apache lucene
Apache luceneApache lucene
Apache lucene
 
IR with lucene
IR with luceneIR with lucene
IR with lucene
 
Tutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component pluginTutorial on developing a Solr search component plugin
Tutorial on developing a Solr search component plugin
 
MongoDB 2.4 and spring data
MongoDB 2.4 and spring dataMongoDB 2.4 and spring data
MongoDB 2.4 and spring data
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 
SharePoint TechCon 2009 - 801
SharePoint TechCon 2009 - 801SharePoint TechCon 2009 - 801
SharePoint TechCon 2009 - 801
 
Lucene
LuceneLucene
Lucene
 
Introduction to SharePoint 2013 REST API
Introduction to SharePoint 2013 REST APIIntroduction to SharePoint 2013 REST API
Introduction to SharePoint 2013 REST API
 
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
Elasticsearch Tutorial | Getting Started with Elasticsearch | ELK Stack Train...
 
Munching & crunching - Lucene index post-processing
Munching & crunching - Lucene index post-processingMunching & crunching - Lucene index post-processing
Munching & crunching - Lucene index post-processing
 
Battle of the giants: Apache Solr vs ElasticSearch
Battle of the giants: Apache Solr vs ElasticSearchBattle of the giants: Apache Solr vs ElasticSearch
Battle of the giants: Apache Solr vs ElasticSearch
 
Apache Lucene intro - Breizhcamp 2015
Apache Lucene intro - Breizhcamp 2015Apache Lucene intro - Breizhcamp 2015
Apache Lucene intro - Breizhcamp 2015
 
Faceted Search with Lucene
Faceted Search with LuceneFaceted Search with Lucene
Faceted Search with Lucene
 
Intro to Elasticsearch
Intro to ElasticsearchIntro to Elasticsearch
Intro to Elasticsearch
 

Ähnlich wie Александр Третьяков: "Spring Data JPA and MongoDB"

Spring data presentation
Spring data presentationSpring data presentation
Spring data presentationOleksii Usyk
 
A first Draft to Java Configuration
A first Draft to Java ConfigurationA first Draft to Java Configuration
A first Draft to Java ConfigurationAnatole Tresch
 
Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @...
Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @...Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @...
Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @...Neo4j
 
Thomas risberg mongosv-2012-spring-data-cloud-foundry
Thomas risberg mongosv-2012-spring-data-cloud-foundryThomas risberg mongosv-2012-spring-data-cloud-foundry
Thomas risberg mongosv-2012-spring-data-cloud-foundrytrisberg
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreIMC Institute
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring DataEric Bottard
 
Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!Oliver Gierke
 
JeeConf 2018 - The anatomy of Spring Data
JeeConf 2018 - The anatomy of Spring DataJeeConf 2018 - The anatomy of Spring Data
JeeConf 2018 - The anatomy of Spring DataMaksym Govorischev
 
Spring 4-groovy
Spring 4-groovySpring 4-groovy
Spring 4-groovyGR8Conf
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code FirstJames Johnson
 
Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Igor Anishchenko
 

Ähnlich wie Александр Третьяков: "Spring Data JPA and MongoDB" (20)

Spring data presentation
Spring data presentationSpring data presentation
Spring data presentation
 
A first Draft to Java Configuration
A first Draft to Java ConfigurationA first Draft to Java Configuration
A first Draft to Java Configuration
 
Apache Kite
Apache KiteApache Kite
Apache Kite
 
Spring.io
Spring.ioSpring.io
Spring.io
 
Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @...
Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @...Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @...
Object Graph Mapping with Spring Data Neo4j 3 - Nicki Watt & Michael Hunger @...
 
Spring Data in 10 minutes
Spring Data in 10 minutesSpring Data in 10 minutes
Spring Data in 10 minutes
 
Thomas risberg mongosv-2012-spring-data-cloud-foundry
Thomas risberg mongosv-2012-spring-data-cloud-foundryThomas risberg mongosv-2012-spring-data-cloud-foundry
Thomas risberg mongosv-2012-spring-data-cloud-foundry
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : Datastore
 
L04 base patterns
L04 base patternsL04 base patterns
L04 base patterns
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring Data
 
Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!
 
MAVRL Workshop 2014 - Python Materials Genomics (pymatgen)
MAVRL Workshop 2014 - Python Materials Genomics (pymatgen)MAVRL Workshop 2014 - Python Materials Genomics (pymatgen)
MAVRL Workshop 2014 - Python Materials Genomics (pymatgen)
 
Data access
Data accessData access
Data access
 
JeeConf 2018 - The anatomy of Spring Data
JeeConf 2018 - The anatomy of Spring DataJeeConf 2018 - The anatomy of Spring Data
JeeConf 2018 - The anatomy of Spring Data
 
Spring 4-groovy
Spring 4-groovySpring 4-groovy
Spring 4-groovy
 
WPF and Databases
WPF and DatabasesWPF and Databases
WPF and Databases
 
La sql
La sqlLa sql
La sql
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)Spring Data - Intro (Odessa Java TechTalks)
Spring Data - Intro (Odessa Java TechTalks)
 

Mehr von Anna Shymchenko

Константин Маркович: "Creating modular application using Spring Boot "
Константин Маркович: "Creating modular application using Spring Boot "Константин Маркович: "Creating modular application using Spring Boot "
Константин Маркович: "Creating modular application using Spring Boot "Anna Shymchenko
 
Евгений Бова: "Modularity in Java: introduction to Jigsaw through the prism o...
Евгений Бова: "Modularity in Java: introduction to Jigsaw through the prism o...Евгений Бова: "Modularity in Java: introduction to Jigsaw through the prism o...
Евгений Бова: "Modularity in Java: introduction to Jigsaw through the prism o...Anna Shymchenko
 
Евгений Руднев: "Programmers Approach to Error Handling"
Евгений Руднев: "Programmers Approach to Error Handling"Евгений Руднев: "Programmers Approach to Error Handling"
Евгений Руднев: "Programmers Approach to Error Handling"Anna Shymchenko
 
Александр Куцан: "Static Code Analysis in C++"
Александр Куцан: "Static Code Analysis in C++" Александр Куцан: "Static Code Analysis in C++"
Александр Куцан: "Static Code Analysis in C++" Anna Shymchenko
 
Алесей Решта: “Robotics Sport & Luxoft Open Robotics Club”
Алесей Решта: “Robotics Sport & Luxoft Open Robotics Club” Алесей Решта: “Robotics Sport & Luxoft Open Robotics Club”
Алесей Решта: “Robotics Sport & Luxoft Open Robotics Club” Anna Shymchenko
 
Орхан Гасимов: "Reactive Applications in Java with Akka"
Орхан Гасимов: "Reactive Applications in Java with Akka"Орхан Гасимов: "Reactive Applications in Java with Akka"
Орхан Гасимов: "Reactive Applications in Java with Akka"Anna Shymchenko
 
Евгений Хыст: "Server-Side Geo-Clustering Based on Geohash"
Евгений Хыст: "Server-Side Geo-Clustering Based on Geohash"Евгений Хыст: "Server-Side Geo-Clustering Based on Geohash"
Евгений Хыст: "Server-Side Geo-Clustering Based on Geohash"Anna Shymchenko
 
Денис Прокопюк: “JMX in Java EE applications”
Денис Прокопюк: “JMX in Java EE applications”Денис Прокопюк: “JMX in Java EE applications”
Денис Прокопюк: “JMX in Java EE applications”Anna Shymchenko
 
Роман Яворский "Introduction to DevOps"
Роман Яворский "Introduction to DevOps"Роман Яворский "Introduction to DevOps"
Роман Яворский "Introduction to DevOps"Anna Shymchenko
 
Максим Сабарня “NoSQL: Not only SQL in developer’s life”
Максим Сабарня “NoSQL: Not only SQL in developer’s life” Максим Сабарня “NoSQL: Not only SQL in developer’s life”
Максим Сабарня “NoSQL: Not only SQL in developer’s life” Anna Shymchenko
 
Андрей Лисниченко "SQL Injection"
Андрей Лисниченко "SQL Injection"Андрей Лисниченко "SQL Injection"
Андрей Лисниченко "SQL Injection"Anna Shymchenko
 
Светлана Мухина "Metrics on agile projects"
Светлана Мухина "Metrics on agile projects"Светлана Мухина "Metrics on agile projects"
Светлана Мухина "Metrics on agile projects"Anna Shymchenko
 
Андрей Слободяник "Test driven development using mockito"
Андрей Слободяник "Test driven development using mockito"Андрей Слободяник "Test driven development using mockito"
Андрей Слободяник "Test driven development using mockito"Anna Shymchenko
 
Евгений Хыст "Application performance database related problems"
Евгений Хыст "Application performance database related problems"Евгений Хыст "Application performance database related problems"
Евгений Хыст "Application performance database related problems"Anna Shymchenko
 
Даурен Муса “IBM WebSphere - expensive but effective”
Даурен Муса “IBM WebSphere - expensive but effective” Даурен Муса “IBM WebSphere - expensive but effective”
Даурен Муса “IBM WebSphere - expensive but effective” Anna Shymchenko
 
Александр Пашинский "Reinventing Design Patterns with Java 8"
Александр Пашинский "Reinventing Design Patterns with Java 8"Александр Пашинский "Reinventing Design Patterns with Java 8"
Александр Пашинский "Reinventing Design Patterns with Java 8"Anna Shymchenko
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Anna Shymchenko
 
Event-driven architecture with Java technology stack
Event-driven architecture with Java technology stackEvent-driven architecture with Java technology stack
Event-driven architecture with Java technology stackAnna Shymchenko
 
Do we need SOLID principles during software development?
Do we need SOLID principles during software development?Do we need SOLID principles during software development?
Do we need SOLID principles during software development?Anna Shymchenko
 
Guava - Elements of Functional Programming
Guava - Elements of Functional Programming Guava - Elements of Functional Programming
Guava - Elements of Functional Programming Anna Shymchenko
 

Mehr von Anna Shymchenko (20)

Константин Маркович: "Creating modular application using Spring Boot "
Константин Маркович: "Creating modular application using Spring Boot "Константин Маркович: "Creating modular application using Spring Boot "
Константин Маркович: "Creating modular application using Spring Boot "
 
Евгений Бова: "Modularity in Java: introduction to Jigsaw through the prism o...
Евгений Бова: "Modularity in Java: introduction to Jigsaw through the prism o...Евгений Бова: "Modularity in Java: introduction to Jigsaw through the prism o...
Евгений Бова: "Modularity in Java: introduction to Jigsaw through the prism o...
 
Евгений Руднев: "Programmers Approach to Error Handling"
Евгений Руднев: "Programmers Approach to Error Handling"Евгений Руднев: "Programmers Approach to Error Handling"
Евгений Руднев: "Programmers Approach to Error Handling"
 
Александр Куцан: "Static Code Analysis in C++"
Александр Куцан: "Static Code Analysis in C++" Александр Куцан: "Static Code Analysis in C++"
Александр Куцан: "Static Code Analysis in C++"
 
Алесей Решта: “Robotics Sport & Luxoft Open Robotics Club”
Алесей Решта: “Robotics Sport & Luxoft Open Robotics Club” Алесей Решта: “Robotics Sport & Luxoft Open Robotics Club”
Алесей Решта: “Robotics Sport & Luxoft Open Robotics Club”
 
Орхан Гасимов: "Reactive Applications in Java with Akka"
Орхан Гасимов: "Reactive Applications in Java with Akka"Орхан Гасимов: "Reactive Applications in Java with Akka"
Орхан Гасимов: "Reactive Applications in Java with Akka"
 
Евгений Хыст: "Server-Side Geo-Clustering Based on Geohash"
Евгений Хыст: "Server-Side Geo-Clustering Based on Geohash"Евгений Хыст: "Server-Side Geo-Clustering Based on Geohash"
Евгений Хыст: "Server-Side Geo-Clustering Based on Geohash"
 
Денис Прокопюк: “JMX in Java EE applications”
Денис Прокопюк: “JMX in Java EE applications”Денис Прокопюк: “JMX in Java EE applications”
Денис Прокопюк: “JMX in Java EE applications”
 
Роман Яворский "Introduction to DevOps"
Роман Яворский "Introduction to DevOps"Роман Яворский "Introduction to DevOps"
Роман Яворский "Introduction to DevOps"
 
Максим Сабарня “NoSQL: Not only SQL in developer’s life”
Максим Сабарня “NoSQL: Not only SQL in developer’s life” Максим Сабарня “NoSQL: Not only SQL in developer’s life”
Максим Сабарня “NoSQL: Not only SQL in developer’s life”
 
Андрей Лисниченко "SQL Injection"
Андрей Лисниченко "SQL Injection"Андрей Лисниченко "SQL Injection"
Андрей Лисниченко "SQL Injection"
 
Светлана Мухина "Metrics on agile projects"
Светлана Мухина "Metrics on agile projects"Светлана Мухина "Metrics on agile projects"
Светлана Мухина "Metrics on agile projects"
 
Андрей Слободяник "Test driven development using mockito"
Андрей Слободяник "Test driven development using mockito"Андрей Слободяник "Test driven development using mockito"
Андрей Слободяник "Test driven development using mockito"
 
Евгений Хыст "Application performance database related problems"
Евгений Хыст "Application performance database related problems"Евгений Хыст "Application performance database related problems"
Евгений Хыст "Application performance database related problems"
 
Даурен Муса “IBM WebSphere - expensive but effective”
Даурен Муса “IBM WebSphere - expensive but effective” Даурен Муса “IBM WebSphere - expensive but effective”
Даурен Муса “IBM WebSphere - expensive but effective”
 
Александр Пашинский "Reinventing Design Patterns with Java 8"
Александр Пашинский "Reinventing Design Patterns with Java 8"Александр Пашинский "Reinventing Design Patterns with Java 8"
Александр Пашинский "Reinventing Design Patterns with Java 8"
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"
 
Event-driven architecture with Java technology stack
Event-driven architecture with Java technology stackEvent-driven architecture with Java technology stack
Event-driven architecture with Java technology stack
 
Do we need SOLID principles during software development?
Do we need SOLID principles during software development?Do we need SOLID principles during software development?
Do we need SOLID principles during software development?
 
Guava - Elements of Functional Programming
Guava - Elements of Functional Programming Guava - Elements of Functional Programming
Guava - Elements of Functional Programming
 

Александр Третьяков: "Spring Data JPA and MongoDB"

  • 1. Evolution of persistence Spring Data Alexandr Tretyakov Dnipropetrovs’k 31 July 2014
  • 2. 2 Agenda  The NoSQL landscape  Spring Data JPA  MongoDB in a nutshell  Spring Data MongoDB 4 August 2014
  • 3. 34 August 2014 The NoSQL landscape
  • 6. 6 Project goals 4 August 2014  Spring Data provides a familiar and consistent Spring-based programming model for NoSQL and relational stores while retaining store-specific features and capabilities.
  • 7. 7 Spring Data sub-projects 4 August 2014  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. 8 Don't repeat the DAO 4 August 2014 public 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. 9 Don't repeat the DAO 4 August 2014 public 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. 10 Don't repeat the DAO 4 August 2014 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. 11 Spring Data managed DAO 4 August 2014 Repository 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. 12 Spring Data managed DAO 4 August 2014 @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. 13 Spring Data JPA features 4 August 2014  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. 14 Configuration (XML) 4 August 2014 <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. 15 Configuration 4 August 2014  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. 16 Querying and fine-tuning the queries 4 August 2014  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. 17 Custom method implementations 4 August 2014  Add interface for custom repository functionality interface CustomRepository { public void customMethod(DomainClass domainClass); }  Add implementation of custom repository functionality class CustomRepositoryImpl implements CustomRepository { public void customMethod(DomainClass domainClass) { // Your custom implementation } }  Change your basic repository interface public interface BasicUserRepository extends CrudRepository<DomainClass, IdClass>, CustomRepository { // Declare other query methods here }
  • 18. 18 Fine-tuning repository definition 4 August 2014  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. 19 QueryDSL integration 4 August 2014  Set up maven-apt-plugin with appropriate processor – QuerydslAnnotationProcessor – JPAAnnotationProcessor – HibernateAnnotationProcessor – JDOAnnotationProcessor – MongoAnnotationProcessor  Extend QueryDslPredicateExecutor in your repository interface
  • 20. 20 Spring Data JPA weaknesses 4 August 2014  Less of freedom, less flexibility and more conventions (arguably)  Оne more layer, hiding details (arguably)
  • 21. 214 August 2014 MongoDB in a nutshell
  • 22. 22 Terminology 4 August 2014 RDBMS MongoDB Database Database Table Collection Row(s) JSON Document Row(s) Field Index Index Join Embedded documents & linking
  • 23. 23 Document data model 4 August 2014  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. 24 Key features 4 August 2014  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
  • 27. 27 MongoDB Java Driver 4 August 2014 Mongo 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);
  • 28. 284 August 2014 Spring Data MongoDB
  • 29. 29 Spring Data MongoDB feautures 4 August 2014  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. 30 Spring data mapping 4 August 2014  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. 31 Configuration (XML) 4 August 2014 <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. 32 Spring Data MongoDB repositories 4 August 2014  Spring Data MongoDB repositories and query derivation mechanism works as well as for JPA module (Obviously except for @Query syntax)
  • 33. 33 MongoDB JSON based query 4 August 2014  Placeholders ?* lets you substitute the value from the method arguments into the JSON query string public 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 object public interface PersonRepository extends MongoRepository <Person, String> @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") List<Person> findByThePersonsFirstname(String firstname); }
  • 34. 34 Spring Data MongoDB weaknesses 4 August 2014  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
  • 35. 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