spring data for jjug for cross conference fall

37
© 2011 VMware Inc. All rights reserved クラウド時代のSpring Frameworkの新たな試み ~Spring Dataの全貌~ 2012年11月10日 ヴイエムウェア株式会社 クラウドアプリケーションプラットフォーム テクニカル・エバンジェリスト 池田 俊彦

Upload: toshihiko-ikeda

Post on 28-May-2015

1.275 views

Category:

Documents


0 download

TRANSCRIPT

  • 1. Spring FrameworkSpring Data 2012 2011 VMware Inc. All rights reserved

2. twitter:tikeda123VMware Cloud Foundry,Spring 3. Agenda Spring Data Spring Data Overview 3Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 4. DataBase Database Scaling optionsCentralized Relational DatastoreFederated (NoSQL) Data Store4Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 5. Hard to create a one size ts all solution Fast Data Big Data Flexible Data vs. BI Cloud -as-a-Service5Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 6. Spring Data OverviewSpring Data Spring Data Document Graph Relational DatabasesData Grid StoresStoresJPAJDBC GemFireMongoDBNeo4J Extensions6Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 7. Spring Data OverviewSpring Data provides a familiar and consistent Spring-base programming model for NoSQL and relational store while retaining store-specic features and capabilities. Spring 7Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 8. Spring Data OverviewSpring 8Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 9. Spring Data OverviewSpring Framework pDependency InjectionDIpAspect-Oriented ProgrammingAOPpPortable Service AbstractionSimpleObjectPortable Service Abstraction9Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 10. Spring Data OverviewSpring Framework DI (Dependency Injection) AOP (Aspect-Oriented Programming)Portable Service Abstraction SpringSpring Data POJO 10Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 11. 12. UIControllerDomain objectsData Access (Hibernate,JPA,etc.)SQL DB12Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 13. UIControllerDomain objectsData Access (Hibernate,JPA,etc.) Relational 13Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 14. UIControllerDomain objectsData Access GemFireJPAMongoDB Neo4J Relational 14Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 15. UIControllerDomain objects Spring Data Repository Relational 15Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 16. Mediates between the domain and data mapping layers using acollection-like interface for accessing domain objects. http://martinfowler.com/eaaCatalog/repository.html http://msdn.microsoft.com/en-us/library/ff649690.aspx 17. Spring Data Repository RDMNoSQL Repository public interface Repository{. } CrudRepository public interface CrudRepositoryextends Repository {. } CRUD(Create-Read-Update-Delete )PagingAndSortingRepositorypublic interface PagingAndSoringRepository extends CrudRepository { .}17Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 18. Spring Data RepositoryJPApublic interface CustomerRepository { Customer save(Customer account); Customer findByEmailAddress(EmailAddress emailAddress);}@Repository@Transactional(readOnly = true)class JpaCustomerRepository implements CustomerRepository {@PersistenceContextprivate EntityManager em;@Override@Transactionalpublic Customer save(Customer customer) {if (customer.getId() == null) { em.persist(customer); return customer; } else { return em.merge(customer); }}@Overridepublic Customer findByEmailAddress(EmailAddress emailAddress) { TypedQuery query = em.createQuery( "select c from Customer c where c.emailAddress= :emailAddress", Customer.class);query.setParameter("emailAddress", emailAddress);return query.getSingleResult();}}18Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 19. Spring Data RepositorySpring Datapublic interface CustomerRepository extends Repository{ @Transaction(timeout = 60)Customer save(Customer account); Customer findByEmailAddress(EmailAddress emailAddress);}@Transaction19Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 20. naming conventions public interface PagingAndSoringRepository extends CrudRepository { Iterable findAll(Sort sort); Page findAll(Pageable pageable);}public interface PersonRepository extends CrudRepository { // Finder for a single entityPerson findByEmailAddress(String emailAddress);// Finder for a multiple entitiesList findByLastnameLike(String lastName); // Finder with paginationPage findByFirstnameLike(String firstName, Pageable page);}20Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 21. Spring Data RepositoryJPA Naming convertions Query annotation JPA named query (@NamedQuery) JPAJPQLMongoDBGeoSptial,Neo4cypherpublic interface PersonRepository extends CrudRepository {// previous methods omitted...@Query("select p from Person p where p.emailAddress = ?1) Person findByEmailAddress(String emailAddress);@Query("select p from Person p where p.firstname = :firstname or p.lastname = :lastname") PersonfindByLastnameOrFirstname( @Param("lastname") String lastname, @Param("firstname") String firstname);}21Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 22. Spring Data RepositorySpring Data Repositoryxml namespaceJPAmongodbneo4jJavaCong@Configuration@EnableJpaRepositoriesclass ApplicationConfig {}JPAmongodb@EnableMongoRepositoriesneo4j@EnableNeo4jRepositories22Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 23. Spring Data RepositorySpring Data RepositoryCrudRepositoryQuerypublic interface ProductRepository extends CrudRepository { Page findByDescriptionContaining(String description, Pageable pageable);@Query("select p from Product p where p.attributes[?1] = ?2")List findByAttributeAndValue(String attribute, String value);}@AutowiredProductRepository repository;@Testpublic void lookupProductsByDescription() {Pageable pageable = new PageRequest(0, 1, Direction.DESC, "name");Page page = repository.findByDescriptionContaining("Apple", pageable);assertThat(page.getContent(), hasSize(1));assertThat(page, Matchers. hasItems(named(iPad))); }23Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 24. Spring Data RepositorySQL,NoSQL) boiler-plate code24Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 25. 25Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 26. Java public class Person{ private BigInteger id; private String rstname, lastname; private EmailAddress emailAdress; ..} Relational 26Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 27. DB(JPA) PERSON @EntityId fnamelname(INTEGAR)(VARCHAR)(VARCHAR)@Table(name=CPERSON)public class Person{@Idprivate int Id;O/R @Column(name=fname) ADDRESS citystreet private String Firstname;(VARCHAR) (VARCHAR) @Column(name=lname)private String Lastname;@OneToManyprivate List Addres;}27Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 28. MongoDB)@Document(collection=CPERSON)public class Person{{ Id:100fname : Toshihiko",@Idlname : Ikeda",private int Id;addresses : [ {city : Musashino",street : yamanote-3-2-6" } ] } @Field(fname)private String Firstname;@Field(lname)private String Lastname;private List Address;}28Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 29. Spring Data public void mongoBasic() throws Exception {Mongo mongo = new Mongo();DB db = mongo.getDB("database");DBCollection customers = db.getCollection("customers");DBObject address = new BasicDBObject("city", Kobe");address.put("street", "Broadway");DBObject addresses = new BasicDBList();((BasicDBList) addresses).add(address);DBObject customer = new BasicDBObject("firstname", "Tsuyoshi");customer.put("lastname", "Miyake");ustomer.put("addresses", addresses);customers.insert(customer);System.out.println(customers.findOne()); } Insert / Update () POJO -> DBObject mongo.getDatabase(...).getCollection(...).insert(...)29Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 30. DB@NodeEntity firstname : Mike public class Person{@GraphIdPersonprivate int Id; Address :private String Firstname;firstname :Midori-cho firstname : private String Lastname; ToshihikoJohn@RelatedTo(type = ADDRESS",direction = Direction.OUTGOING)private List Address;}30Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 31. DB Spring Data public class Person {private final Node underlyingNode;public Person(final Node node) { underlyingNode = node;}public Node getUnderlyingNode() {return underlyingNode;}public final String getName() {return (String) underlyingNode.getProperty(name); }public void setName(final String name) {underlyingNode.setProperty(name, name);} } TypeSafeunderlyingNode.getProperty(name);31Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 32. POJO 32Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 33. 33Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 34. Exception Translation Spring Data Repository JPA MongoTemplateNeo4jTemplateMongoDB driverNeo4J driver Relational 34Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 35. SpringSpringPOJO SQL,NoSQL) boiler-plate code POJO 35Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents . 36. Spring Data12/9 18:0021:00 VMware Spring Data URL http://atnd.org/events/34055 Engineer at SpringSourceOliver Gierke is engineer at SpringSource, a division of VMware,project lead of the Spring Data JPA module and involved into otherSpring Data modules (e.g. MongoDB) as well. He has been intodeveloping enterprise applications and open source projects forover 6 years now. His working focus is centered around softwarearchitecture, Spring and persistence technologies. He is regularlyspeaking at German and international conferences as well asauthor of technology articles. Oliver Gierke36Copyright 2012 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents .