54
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission. An Introduction to Spring Data Oliver Gierke - SpringSource, a division of VMware

An Introduction to Spring Data

Embed Size (px)

DESCRIPTION

Introduction into Spring Data, SpringOne 2011

Citation preview

Page 1: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

An Introduction to Spring Data

Oliver Gierke - SpringSource, a division of VMware

Page 2: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Oliver Gierke

Spring DataCore/JPA/MongoDB

[email protected]

2

Page 3: An Introduction to Spring Data

What to expect?

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Page 4: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Why?

Page 5: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Why?

How?

Page 6: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Why?

How?

What?

Page 7: An Introduction to Spring Data

5

A Developer‘s View

Page 8: An Introduction to Spring Data

What to expect?NOT!

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Page 9: An Introduction to Spring Data

What to expect? NOT!

7

Page 10: An Introduction to Spring Data

Retrospect

Page 11: An Introduction to Spring Data

Relational databases

Page 12: An Introduction to Spring Data

Cloud

Page 13: An Introduction to Spring Data

Scaling

Page 14: An Introduction to Spring Data

Data structures

Page 15: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

(No)SQLRedis

Riak

MongoDB

Cassandra

CouchDB

Neo4JHBase

SimpleDB

OrientDB

MembaseHibari Voldemort

Sones

Page 16: An Introduction to Spring Data

Graphs

Page 17: An Introduction to Spring Data

Documents

Page 18: An Introduction to Spring Data

Column families

Page 19: An Introduction to Spring Data

Key Value

Page 20: An Introduction to Spring Data

Forest for the woods?

Page 21: An Introduction to Spring Data

19

A Developer‘s View

Page 22: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

There‘s someSpring for that!

Page 23: An Introduction to Spring Data

Spring Data

Page 24: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

"… provide a familiar and consistent Spring-based programming model while not over-abstracting custom traits of the specific store.

Page 25: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

JPAJDBC

Page 26: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

JPAJDBC

Page 27: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

JPAJDBC

Page 28: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

JPAJDBC

Page 29: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

JPAJDBC

Page 30: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

JPAJDBC

Page 31: An Introduction to Spring Data

Building blocks

Page 32: An Introduction to Spring Data

Spring

Page 33: An Introduction to Spring Data

Mapping

Page 34: An Introduction to Spring Data

32

Entity mapping

@Documentclass Person {

@Id private BigInteger id;@Indexed private String firstname, lastname;@Field(„email“) private String emailAddress;@DBRef private Set<Person> colleagues;

public Person(String firstname) { … }

@PersistenceConstructorpublic Person(String firstname, String lastname) { … }

…}

Page 35: An Introduction to Spring Data

Templates

Page 36: An Introduction to Spring Data

34

MongoOperations / -Template

public interface MongoOperations {

// Generic callback-accepting methods <T> T execute(DbCallback<T> action);

<T> T execute(Class<?> entityClass, CollectionCallback<T> action);<T> T execute(String collectionName, CollectionCallback<T> action);

// Higher level access methods<T> List<T> find(Query query, Class<T> entityClass);void save(Object objectToSave, String collectionName);WriteResult updateFirst(Query query, Update update, Class<?>

entityClass);

// Geo API<T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass);

}

Page 37: An Introduction to Spring Data

35

MongoTemplate usage

// Setup infrastructureMongo mongo = new Mongo();MongoDbFactory factory = new SimpleMongoDbFactory(mongo, „foo“);MongoTemplate template = new MongoTemplate(factory);

// Create and save entityPerson dave = new Person(„Dave“, „Matthews“);dave.setEmailAddress(„[email protected]“);template.save(person);

// Query entityQuery query = new Query(new Criteria(„emailAddress“)

.is(„[email protected]“));assertThat(template.find(query), is(dave));

Page 38: An Introduction to Spring Data

Repositories

Page 39: An Introduction to Spring Data

37

Repositories

public interface PersonRepository extends Repository<Person, BigInteger>{

// Finder for a single entityPerson findByEmailAddress(String emailAddress);

// Finder for multiple entitiesList<Person> findByLastnameLike(String lastname);

// Finder with paginationPage<Person> findByFirstnameLike(String firstname, Pageable page);

// Geospatial queriesList<Person> findByLocationNear(Point location, Distance distance);GeoResults<Person> findByLocationNear(Point location);

}

Page 40: An Introduction to Spring Data

38

Repositories usage

<mongo:repositories base-package=„com.acme.repositories“ />

@Componentpublic class MyClient {

@Autowiredprivate PersonRepository repository;

public List<Person> doSomething() {

Point point = new Point(43.7, 48.8);Distance distance = new Distance(200, Metrics.KILOMETERS);return repository.findByLocationNear(point, distance);

}}

Page 41: An Introduction to Spring Data

Repositories

39

Querydsl

Page 42: An Introduction to Spring Data

40

MongoTemplate usage

public interface QueryDslPredicateExecutor<T> {T findOne(Predicate predicate);List<T> findAll(Predicate predicate);

}

public interface PersonRepository extends Repository<Person, ObjectId>,QueryDslPredicateExecutor { … }

QPerson $ = QPerson.person;BooleanExpression left = $.lastname.contains(„eth“);BooleanExpression right = $.firstname.is(„Carter“);

List<Person> result = repository.findAll(left.or(right));assertThat(result.size(), is(2));assertThat(result, hasItems(dave, carter));

Page 43: An Introduction to Spring Data

JPA

Page 44: An Introduction to Spring Data

42

JPA entity mapping

@Entityclass Person {

@Id@GeneratedValue(strategy=GenerationType.AUTO)private BigInteger id;private String firstname, lastname;

@Column(name=„email“) private String emailAddress;

@OneToMany private Set<Person> colleagues;

}

Page 45: An Introduction to Spring Data

43

Repository

public interface PersonRepository extends Repository<Person, BigInteger>{

// Finder for a single entityPerson findByEmailAddress(String emailAddress);

// Finder for multiple entitiesList<Person> findByLastnameLike(String lastname);

// Finder with paginationPage<Person> findByFirstnameLike(String firstname, Pageable page);

}

<jpa:repositories base-package=„com.acme.repositories“ />

Page 46: An Introduction to Spring Data

Wrap up

Page 47: An Introduction to Spring Data

• Sophisticated mapping support• Templates• Repositories• Querydsl• Spring namespace• Geospatial support• Cross-store persistence

45

Wrap up

Page 48: An Introduction to Spring Data

46

Upcoming Talks

• Today– 10:15 - Spring Data Gemfire– 12:45 - Spring Data MongoDB & CloudFoundry– 4:30 - Spring Data Neo4J

• Thursday– 8:30 - Spring Data JPA & Repositories– 10:15 - Polyglot persistence

• Friday– 8:30 - SQLFire

Page 49: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Play with it

Page 50: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Participate

Page 51: An Introduction to Spring Data

© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Spread the word

Page 52: An Introduction to Spring Data

Questions?

Page 54: An Introduction to Spring Data

• Building blocks - http://www.sxc.hu/photo/297189• Columns - http://www.sxc.hu/photo/1033540• Dilemma - http://www.sxc.hu/photo/125069• Forest - http://www.sxc.hu/photo/1274066• Glasses - http://www.sxc.hu/photo/696003• Mapping - http://www.sxc.hu/photo/1253374• Nails - http://www.sxc.hu/photo/933587• Questions - http://www.sxc.hu/photo/860327• Repository - http://www.sxc.hu/photo/1042408• Retrospect - http://www.sxc.hu/photo/921297• Spreadsheet - http://www.sxc.hu/photo/338505• Spring - http://www.sxc.hu/photo/1291358• Umbrella - http://www.sxc.hu/photo/1364634• Template - http://www.sxc.hu/photo/619819• Wrap up - http://www.sxc.hu/photo/922227

52

Sources