S2G Conference, London May 31st – Notes on Spring Data, Oliver Gierke

This was the lecture I was most interested in. Given the interest in NoSQL, as well as the FUD and general handwaving from the industry on this topic I really wanted to see what SpringSource were making of it.

2 part lecture : motivation to why and then examples

Motivation
Until recently, we have stored everything in RDBMS. This is sub-optimal for objects but especially or trees or graphs or networks. The last couple of years has seen th rise of NoSQL is the umbrella term for the desire

So which do I use? And if I do have a shortlist, how do I interact with them. Well there’s some Spring for that! The Spring Data Project –  aims to provides a familiar and constant Spring based programming model while not over-abstracting custom traits of the specific store. The last point is emphasised because an overly flat abstraction means that you would lose the things that make each implementation special.

The First point is just the usage of familiar spring idioms like Templates and Repositores. Riak and Redis are quite well supported. MongoDB and CouchDB is quite well supported.Haddop and Neo4J are also supported. JDBC and JPA is aso supported since this project is about Data, not just NoSQL.

The examples for this talk are based on MongoDB. JSON based with nested KV pairs  with consistency within the document.

The building blocks of Spring Data: Spring Core (DI, AOP, Namespaces, JMX, JDBC), Templates for each implementation for resource management (connections) and exception handling – also some convenience methods for simple queries, Mapping support  via annotations to map the domain model to the store. Repositories layer – finder methods and query methods like in Spring Roo.

Examples are all on github. – org.springframework.data.mongodb.examples.music

In the mongo example, the domain model is decorated with @Document annotation and the index member decorated with @Id

A kv pair is a field and it’s value. By default the mapping are with the member name but these can be overridden. @DBRef allows you to connect to a separate collection in another DB. Multiple constructors can be filtered with @PersistenceConstructor

To use it you just need to declare the implementation namespace and then create a template. Like RestTemplate, the templates are underpinned by the equivalent *Operations Interface.

Repositories for CredRespoitory  for finder methods. NBoth names and locations (co-ordinates) which auto-creates the repositories which give you a lot of power for simple queries like findOne(Id) find<Member><Property>[<Keywords>].

THe auto-gen stuff is done at context startup so you don’t have to wait until runtime for it to fail.

What about complex queries where you don’t want ugly names. Annotations of course! You can have queries in the annotations

SPring Data supports pagination out of the box. All you need is  Pageable argument where the PageRquest is the implementation. Declare the result type as Page andd you get Context like isLast and getTotalPages.

QueryDSL open source project?

Cross storage persistence is also possible with a nice annotation model that makes it pretty seemless.

Great lecture.

Leave a Reply

Your email address will not be published. Required fields are marked *