+ All Categories
Home > Documents > Hands On Spring Data

Hands On Spring Data

Date post: 09-May-2015
Category:
Upload: eric-bottard
View: 1,431 times
Download: 6 times
Share this document with a friend
Description:
Slides to the Hands On Spring Data lab, presented in Paris on Dec 10th, 2012. Code exercises are here: https://github.com/ericbottard/hands-on-spring-data
39
© 2012 SpringSource, by VMware Hands On Spring Data Eric Bottard, Developer Advocate, VMware @ebottard Florent Biville, Developer, Lateral Thoughts @fbiville
Transcript
Page 1: Hands On Spring Data

© 2012 SpringSource, by VMware

Hands On Spring Data

Eric Bottard, Developer Advocate, VMware@ebottardFlorent Biville, Developer, Lateral Thoughts@fbiville

Page 2: Hands On Spring Data

Eric BOTTARD

• Developer Advocate VMware• @ebottard• [email protected]

2

Page 3: Hands On Spring Data

Florent BIVILLE

• Développeur chez LateralThoughts• @fbiville

3

Page 4: Hands On Spring Data

Spring Data is an umbrella project which aims to provide a familiar and consistent Spring-based programming model for for new datastores while retaining store-specific features and capabilities

Page 5: Hands On Spring Data

Relational

JDBCJPA

Page 6: Hands On Spring Data

Document store

MongoDBCouchDB

Page 7: Hands On Spring Data

Column oriented

HBaseCassandra

Page 8: Hands On Spring Data

Graph

Neo4jOrientDB

Page 9: Hands On Spring Data

Data Grids

GemFireCoherenceTerracotta

Page 10: Hands On Spring Data

Key Value

RedisRiak

Page 11: Hands On Spring Data

Big Data

Hadoop

Page 12: Hands On Spring Data

Templates

Page 13: Hands On Spring Data

Mapping

Page 14: Hands On Spring Data

Mapping

• Tells the framework how to store object properties and relationships

• Favors convention over configuration• Does not try to shoehorn one model into another (e.g.

does not use JPA annotations for Mongo)

14

Page 15: Hands On Spring Data

Example: MongoDB@Document@CompoundIndexes({ @CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}")})public class Person<T extends Address> {

@Id private String id;

@Indexed(unique = true) private Integer ssn;

@Field("fName") private String firstName;

@Indexed private String lastName;

@Transient private Integer accountTotal;

@DBRef private List<Account> accounts;

private T address;

Page 16: Hands On Spring Data

Repositories

Page 17: Hands On Spring Data

Repositories

public interface PersonRepository extends CrudRepository<User, Long> { …

}

No implementation needed!

Page 18: Hands On Spring Data

Magic Finders

Page 19: Hands On Spring Data

Magic Finderspublic interface PersonRepository extends Repository<User, Long> {

List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);

// Enables the distinct flag for the query List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname); List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);

// Enabling ignoring case for an individual property List<Person> findByLastnameIgnoreCase(String lastname); // Enabling ignoring case for all suitable properties List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);

// Enabling static ORDER BY for a query List<Person> findByLastnameOrderByFirstnameAsc(String lastname); List<Person> findByLastnameOrderByFirstnameDesc(String lastname);}

Page 20: Hands On Spring Data

Pagination

Page 21: Hands On Spring Data

Pagination and Dynamic Ordering

public interface PersonRepository extends CrudRepository<User, Long> { Page<User> findByLastname(String lastname, Pageable pageable);

List<User> findByLastname(String lastname, Sort sort);

// NOTE: Pageable embeds a Sort

}

Page 22: Hands On Spring Data

Queries

Page 23: Hands On Spring Data

Explicit Queries

public interface ProductsRepository extends JpaRepository<Product, Long> {

@Query(value = "select p from Product p order by p.rating desc", countQuery = "select count(p) from Product p") Page<Product> findTopRated(Pageable pageable);}

Page 24: Hands On Spring Data

Cross Store support

Page 25: Hands On Spring Data

Query DSL Integration

Page 26: Hands On Spring Data

REST

Page 27: Hands On Spring Data

REST Shell

Page 28: Hands On Spring Data

Customization

Page 29: Hands On Spring Data
Page 30: Hands On Spring Data

JSON

Page 31: Hands On Spring Data

Mongo DB - schema-less document DB

databasesmongo> show dbs;

myDatabase 0.3046578 GBmongo> use myDatabase;

collectionsmongo> show collections;

rockCollectioncolleagueSean

mongo> db.rockCollection.find().pretty(); // last call “prettifies” output

documentsmongo> db.rockCollection.find({name:"The Beatles"}).pretty();

{"_id" : ObjectId("50b7d870744e5ef5eee5224e"),"_class" : "xxx.music.domain.Band","name" : "The Beatles",[...] }

Page 32: Hands On Spring Data

MongoDB: Queries as JSON documents

db.inventory.find({ price:1.99, $or: [ { qty: { $lt: 20 } }, { sale: true } ] })

Page 33: Hands On Spring Data

GridFS - can I haz large objectz?• mongo> show collections;

– fs.chunks– fs.files

• CHUNKS– 256KB portion of a Mongo object (a.k.a. file), stored into

collection “fs.chunks”• FILES

– Metadata about the file stored into “fs.files”: filename, content type ...

• Spring Data brings– GridFsTemplate– GridFsResource (wraps GridFSDBFile)

33

Page 34: Hands On Spring Data
Page 35: Hands On Spring Data

Nodes & Relationships

35

Page 36: Hands On Spring Data

NoSQLstandsFor: "Not Only

SQL"SpringData

HELPS_WITHhow: "it roxx"

Spring

IS_PART_OF

Nodes

Properties

Relationship

Page 37: Hands On Spring Data

Cypher

Page 38: Hands On Spring Data

Neo4J - Cypher Query Language

start doctor=node:characters(character = 'Doctor') match (doctor)<-[:COMPANION_OF]-(companion) -[:APPEARED_IN]->(episode) return companion.character, count(episode) order by count(episode) desc limit 5

Page 39: Hands On Spring Data

Image credits

• keys http://www.sxc.hu/photo/1381979• spiderweb http://www.sxc.hu/photo/1309629• columns http://www.sxc.hu/photo/511217• documents http://www.sxc.hu/photo/913588• umbrella http://www.sxc.hu/photo/1182110• nail http://www.sxc.hu/photo/1101239• relational http://www.sxc.hu/photo/541351• callback http://www.sxc.hu/photo/1134440• repositories http://www.sxc.hu/photo/1352633• pagination http://www.sxc.hu/photo/830250• magic http://www.sxc.hu/photo/829135• cypher http://www.sxc.hu/photo/1118342• mapping http://www.sxc.hu/photo/1147986• grid http://www.sxc.hu/photo/1222068• elephant http://www.sxc.hu/photo/1168187• cross knot http://www.sxc.hu/photo/1396427• rope knot http://www.sxc.hu/photo/1181983• married http://www.sxc.hu/photo/937988• shell http://www.sxc.hu/photo/866402• diesel http://www.sxc.hu/photo/1076436• car tuning http://www.sxc.hu/photo/5877• rest on couch: If you haven’t, go watch Fight Club. NOW.

39


Recommended