3. Definitions
● Persistence
● the action or fact of persisting
● the quality or state of being persistent; especially :
perseverance
● the quality that allows someone to continue doing
something or trying to do something even though it is
difficult or opposed by other people
● the state of occurring or existing beyond the usual,
expected, or normal time
4. Definitions
● Polyglot
● knowing or using several languages
● computer program or script written in a valid form of
multiple programming languages
5. Polyglot – Sample
#define a /*
#<?php
echo "010Hello, world!n";// 2> /dev/null > /dev/null ;
// 2> /dev/null; x=a;
$x=5; // 2> /dev/null ;
if (($x))
// 2> /dev/null; then
return 0;
// 2> /dev/null; fi
#define e ?>
#define b */
#include <stdio.h>
#define main() int main(void)
#define printf printf(
#define true )
#define function
function main()
{
printf "Hello, world!n"true/* 2> /dev/null | grep -v true*/;
return 0;
}
#define c /*
main
#*/
6. What is going on in persistence?
● RDBMS doesn't scale to Internet (millions of users and billions of transactions).
● Ability to interact with multiple data stores depending on specific use cases
● MongoDB or Couchbase for Customer data
● RDBMS for financial transactions
● Elasticsearch, Solr, Cassandra or HBase for audit trails and analytics and
search
● Redis and Gemfire for shared state
● Neo4J, OrientDB for Graph Data
● RDF Stores for Semantic Reasoning
● Hadoop for Big Data
7. Simple JDBC Example
import java.sql.*;
public class SimpleDemoJDBC {
public Person getPerson(String userId, Connection conn) throws SQLException {
Person result = null;
PreparedStatement pstmt = conn.prepareStatement(
"SELECT userId, email, name FROM persons WHERE userId = ?");
pstmt.setString(1, userId);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
result = new Person(rs.getString(1), rs.getString(2), rs.getString(3));
}
rs.close();
pstmt.close();
return result;
}
public void updatePerson(Person person, Connection conn) throws SQLException {
Statement stmt = conn.prepareStatement(
"UPDATE persons SET email = ?, name = ? WHERE userId = ?");
stmt.setString(1, person.getEmail());
stmt.setString(2, person.getName());
stmt.setString(3, person.getUserId());
stmt.executeUpdate();
stmt.close();
}
}
8. SQLJ what happened there?
import java.sql.*;
public class SimpleDemoSQLJ {
public Person getPerson(String userId) throws SQLException {
Person person = new Person();
#sql { SELECT userId, email, name INTO
:person.userId, :person.email, :person.userId
FROM persons WHERE userId = :userId
};
return person;
}
public void updatePerson(Person person) throws SQLException {
#sql { UPDATE persons SET email = :person.email,
name = :person.name
WHERE userId = :person.userId
};
}
}
Syntax may be incorrect
9. MongoDB Sample
DB db = mongo.getDB("MyData");
DBCollection persons = db.getCollection("Persons");
public Person findPerson(String userId) {
Iterable<Document> documents = persons.find(
new Document("userId", userId));
for(Document doc : documents) {
return new Person(doc.get("userId", String.class),
doc.get("name", String.class),
doc.get("email", String.class));
}
return null;
}
public void updatePerson(Person person) {
Document doc = new Document(“userId”, person.getUserId());
doc.put(“email”, person.getEmail());
doc.put(“name”, person.getName());
Document query = new Document("userId", person.getUserId());
Document update = new Document(“$set”, doc);
persons.update(query, update);
}
10. What are we talking about?
● Improve programmer productivity.
● Polyglot persistence using one language.
● The language of Spring Data.
11. Spring for programmer productivity
● Spring does not provide ORM
● Spring does not provide Database
● Spring as inversion of control container
● Spring templates for JMS, JDBC, Hibernate, JPA,
MongoDB and much more.
12. Why Spring Data?
● Best practice indicates you should have Data Access
Components or a Data Service Layer.
● How often do you make simple mistakes interacting with
EntityManager?
● Different API for each store.
● It should be easier...
13. What is Spring Data?
● Spring Data is an open source project at
http://projects.spring.io/spring-data/ managed by Pivotal Software.
● Spring Data relies on Spring Framework.
● Spring Data provides a set of common patterns for persisting data
using existing libraries.
● Spring Data provides a Repository Interface
● Spring Data provides finder methods
● Spring Data provides support for QueryDSL
● Spring Data simplifies persistence
● Spring Data enables polyglot persistence
15. How Spring Data?
● Configure Spring Data for specific technology
● Define Entity as per relevant library or framework
● Declare Repository Interface
● Spring Data provides Repository Implementation
● Spring Framework injects Repository implementation
16. Spring Data Samples – Configuration
● EntityManagerFactory as normal for JPA
<jpa:repositories
base-package="com.springframework.data.demo" />
OR
@EnableJpaRepositories({"com.springframework.data.demo"})
17. Spring Data Sample – Entity
@Entity
public class User {
@Id
Long id;
String fullName;
String gender;
Date dateOfBirth;
}
Getters and Setters removed for brevity
18. Spring Data Sample – Repository
public interface UserRepository
extends CrudRepository<User, Long> {
}
● Crud Repository provides type-safe methods for save,
delete, load
19. Spring Data Sample - Injection
@Service
public class MyDataServiceImpl {
@AutoWired
protected UserRepository userRepo;
public void createUser(User user) {
userRepo.save(user);
}
}
20. Spring Data Sample – Finders
public interface UserRepository
extends CrudRepository<User, Long> {
List<User> findByGender(String genderCode);
List<User> findByDateOfBirthBetween(
Date startDate, Date endDate);
}
● Crud Repository provides query predicates based on method name like:
findByNameOrderByDateOfBirthDesc
findByNameAndGender
● Finder method tokens
● And, Or, Is, Equals, Between, LessThan, LessThanEqual, GreaterThan,
GreaterThanEqual, After, Before, IsNull, IsNotNull, NotNull, Like,
NotLike, StartingWith, EndingWith, Containing, Not, In, NotIn, True,
False, IgnoreCase
21. Spring Data Query
● Specific to technology
● Annotated finder method
@Query("select * from User u where u.gender = ?")
List<User> findUsersForGender(String code)
Getters and Setters removed for brevity
22. Spring Data Sample – QueryDSL
@Entity
@QueryEntity
public class User {
@Id
private Long id;
private String fullName;
private String gender;
private Date dateOfBirth;
}
● QueryDSL generates QUser for creating type-safe query.
class UserRepository extends
QueryDslPredicateExecutor<User>,
CrudRepository<User,Long> {
}
import static Quser.*;
List<User> users = userRepo.findAll(user.gender.eq('M'));
List<User> users = userRepo.findAll(user.fullName.like("%abc%"));
23. Spring Data – Paging and Sorting
class UserRepository extends
PagingAndSortingRepository<User, Long> {
}
...
Pageble page = new PageRequest(1,20);
Page<User> users = userRepo.findAll(user.gender.eq("M"), page);
● Create Pageable in UI and add to method.
● Add Pageble to finders
● Add Sort to finders
● Page<User> can be used to retrieve pages.
● Slice<User> can only retrieve next slice.
24. Spring Data – Notes
● Shared code for multiple technologies like MongoDB,
JPA, Neo4J etc.
● Entity Mapping is usually specific to technology.
● Common Repository methods can be implemented.
● Paging and Sorting support.
25. Spring Data – Demo and
Questions
● JPA With Hibernate
● MongoDB
● QueryDSL
● Demo code at
https://github.com/corneil/spring-data-demo