Weitere ähnliche Inhalte Ähnlich wie Spring Data for JJUG for Cross Conference Fall Ähnlich wie Spring Data for JJUG for Cross Conference Fall (20) Spring Data for JJUG for Cross Conference Fall 1. クラウド時代のSpring Frameworkの新たな試み
~Spring Dataの全貌~
2012年11月10日
ヴイエムウェア株式会社
クラウドアプリケーションプラットフォーム テクニカル・エバンジェリスト
池田 俊彦
© 2011 VMware Inc. All rights reserved
2. ⾃自⼰己紹介
池⽥田俊彦
twitter:tikeda123
VMware Cloud Foundry,Spring関連の
エバンジェリストとして活動
3. Agenda
• はじめに
• Spring Dataとは?
• Spring Data Overview
• リポジトリ
• ドメインマッピング
• テンプレート
• まとめ
3
Copyright © 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 options
Centralized Relational Datastore
Federated (NoSQL) Data Store
4
Copyright © 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 fits all” solution
Fast Data
§ アクセス・パターンの多様化
§ モバイル・アプリの急増
Big Data Flexible Data
§ ペタバイト vs. § ⾮非構造化データ
ギガバイト § 開発者の⽣生産性
§ BIの⼤大衆化
Cloud
§ 仮想化
§ “-‐‑‒as-‐‑‒a-‐‑‒Service”として
5
Copyright © 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 Overview
Spring Dataプロジェクトは複数のサブプロジェクトから構成される。
Spring Data プロジェクト
Document Graph
Relational Databases Data Grid
Stores Stores
JPA JDBC GemFire MongoDB Neo4J
Extensions
6
Copyright © 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 Overview
“Spring Data provides a familiar and consistent
Spring-‐‑‒base programming model for NoSQL
and relational store while retaining store-‐‑‒specific
features and capabilities. “
…慣れ親しんだ⼀一貫したSpringベースのプログラミングモデ
ル…
7
Copyright © 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 Overview
…慣れ親しんだ⼀一貫したSpringベースのプログラミングモデ
ル…
?
?
?
?
8
Copyright © 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 Overview
Spring Framework の特徴
p Dependency Injection
ü 依存性の注⼊入:DI
p Aspect-‐‑‒Oriented Programming
ü アスペクト志向プログラミング:AOP
p Portable Service Abstraction
ü データ永続化、スケジュール、トランザクショ
ンに関する実装とビジネスロジックを疎結合にす
るための抽象化
Simple
Object
Portable Service Abstraction
9
Copyright © 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 Overview
Spring Framework の特徴
DI (Dependency Injection)
AOP (Aspect-‐‑‒Oriented Programming)
Portable Service Abstraction
Springの特徴を活かした
Spring Data
⼀一貫したプログラムモデルの提供
POJOセントリックなデータアクセスモデル
リポジトリ
ドメイン・マッピング
テンプレート
10
Copyright © 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 .
12. 今までのデータアクセスアーキテクチャ
UI
Controller
Domain objects
Data Access (Hibernate,JPA,etc.)
SQL DB
12
Copyright © 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. 今までのデータアクセスアーキテクチャ
UI
Controller
Domain objects
Data Access (Hibernate,JPA,etc.)
Relational
13
Copyright © 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. 複雑でヘテロなデータアクセスアーキテクチャ
UI
Controller
Domain objects
Data Access
GemFire JPA MongoDB Neo4J
Relational
14
Copyright © 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. シンプルで統⼀一感のあるデータアクセスアーキテクチャ
UI
Controller
Domain objects
Spring Data Repository
Relational
15
Copyright © 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 a
collection-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 RDMとNoSQL共通したインタフェースを提供
Repository
public interface Repository<T, ID extends Serializable >{
…….
}
ドメインオブジェクトに対する基本インタフェース
CrudRepository
public interface CrudRepository<T, ID extends Serializable >
extends Repository<T, ID> {
…….
}
ドメインオブジェクトに対するCRUD(Create-‐‑‒Read-‐‑‒Update-‐‑‒Delete )操作を⾏行行うインタフェース
PagingAndSortingRepository
public interface PagingAndSoringRepository<T, ID extends Serializable >
extends CrudRepository<T, ID> {
…….
}
ドメインオブジェクトに対するソート処理理やページ付け処理理操作を⾏行行うインタフェース
17
Copyright © 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 Repository 今までの(JPAのみ)アプローチ
public interface CustomerRepository {
Customer save(Customer account);
Customer findByEmailAddress(EmailAddress emailAddress);
}
@Repository
@Transactional(readOnly = true)
class JpaCustomerRepository implements CustomerRepository {
@PersistenceContext
private EntityManager em;
@Override
@Transactional
public Customer save(Customer customer) {
if (customer.getId() == null) {
em.persist(customer);
return customer;
} else {
return em.merge(customer);
}
}
@Override
public Customer findByEmailAddress(EmailAddress emailAddress) {
TypedQuery<Customer> query = em.createQuery( "select c from Customer c where c.emailAddress
= :emailAddress", Customer.class);
query.setParameter("emailAddress", emailAddress);
return query.getSingleResult();
}
}
18
Copyright © 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 Repository Spring Dataを使ったアプローチ
public interface CustomerRepository extends Repository<Customer Long>{
@Transaction(timeout = 60)
Customer save(Customer account);
Customer findByEmailAddress(EmailAddress emailAddress);
}
インタフェースに使いたいメソッドを定義するだけ。トランザクションを指定する場合は、@Transaction
を記述する。
19
Copyright © 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<T, ID extends Serializable >
extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
public interface PersonRepository extends CrudRepository<Person, BigInteger> {
// Finder for a single entity
Person findByEmailAddress(String emailAddress);
// Finder for a multiple entities
List<Person> findByLastnameLike(String lastName);
// Finder with pagination
Page<Person> findByFirstnameLike(String firstName, Pageable page);
}
20
Copyright © 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 Repository クエリー
JPA クエリメソッド(Naming convertions ベース)
• Query annotation でオーバーライド可能
• JPA named query (@NamedQuery) の参照も可
• JPAの場合、JPQLをアノテーションにより記述、MongoDB(GeoSptialも対応),Neo4(cypher)
も同様に対応可能。
public interface PersonRepository extends CrudRepository<Person,BigInteger> {
// 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") Person
findByLastnameOrFirstname( @Param("lastname") String lastname,
@Param("firstname") String firstname);
}
21
Copyright © 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 Repository Spring Data Repository設定
xml namespaceによる定義
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance”
…….
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="com.acme.**.repository" />
</beans>
上記はJPAの例例、mongodbの場合は<mongo:repositories…/>、neo4jの場合は
<neo4j:repositories…/>
JavaConfigによる定義
@Configuration
@EnableJpaRepositories
class ApplicationConfig {
}
上記はJPAの例例、mongodbの場合は@EnableMongoRepositories、neo4jの場合は
@EnableNeo4jRepositories
22
Copyright © 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 Repository Spring Data Repositoryの使い⽅方
CrudRepositoryインタフェースの拡張及びQueryメソッドの定義
public interface ProductRepository extends CrudRepository<Product, Long> {
Page<Product> findByDescriptionContaining(String description, Pageable pageable);
@Query("select p from Product p where p.attributes[?1] = ?2")
List<Product> findByAttributeAndValue(String attribute, String value);
}
@Autowired
ProductRepository repository;
@Test
public void lookupProductsByDescription() {
Pageable pageable = new PageRequest(0, 1, Direction.DESC, "name");
Page<Product> page = repository.findByDescriptionContaining("Apple", pageable);
assertThat(page.getContent(), hasSize(1));
assertThat(page, Matchers.<Product> hasItems(named(“iPad”)));
……
}
23
Copyright © 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 Repository メリット
共通(SQL,NoSQL)のインタフェースとして利利⽤用
“boiler-‐‑‒plate code(お決まりごとのコード)”の排除
クエリをアノテーションを使って簡単にアノテーションメソ
ッドとして定義
24
Copyright © 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. ドメイン・マッピング
25
Copyright © 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 firstname, lastname;
private EmailAddress emailAdress;
……..
}
マッピング
Relational
26
Copyright © 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
@Entity
Id fname lname
(INTEGAR) (VARCHAR) (VARCHAR)
@Table(name=”CPERSON”)
public class Person{
@Id
private int Id;
O/R
マッピング @Column(name=“fname”)
ADDRESS
city street private String Firstname;
(VARCHAR) (VARCHAR) @Column(name=“lname”)
private String Lastname;
@OneToMany
private List Addres;
}
27
Copyright © 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:”100”
fname : ”Toshihiko", @Id
lname : ”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;
}
28
Copyright © 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(...)。
29
Copyright © 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{
@GraphId
Person
private int Id;
マッピング
Address : private String Firstname;
firstname : Midori-cho
firstname : private String Lastname;
Toshihiko John
@RelatedTo(type = ”ADDRESS",
direction = Direction.OUTGOING)
private List Address;
}
30
Copyright © 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);
}
}
TypeSafeでない記述 underlyingNode.getProperty(“name”);
31
Copyright © 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セントリックなプログラミングが可能
データ構造が違うので、共通したインタフェースは持てないが
アノテーションを使ってデータストアの特質を表現
32
Copyright © 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. テンプレート
33
Copyright © 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 MongoTemplate Neo4jTemplate
MongoDB driver
Neo4J driver
リソースマネジメント
適切なリソースの確保、リソースの解放
Relational
34
Copyright © 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. まとめ
慣れ親しんだ⼀一貫したSpringベースのプログラミングモデル提供
Springの特徴を活かしたPOJOセントリックなデータアクセスモデル
メリット
• 共通(SQL,NoSQL)のインタフェースとして利利⽤用
• “boiler-‐‑‒plate code(お決まりごとのコード)”の排除
リポジトリ • クエリをアノテーションを使って簡単にアノテーションメ
ソッドとして定義
• POJOセントリックなプログラミングが可能
ドメイン・マッピング • データ構造が違うので、共通したインタフェースは持てな
いがアノテーションを使ってデータストアの特質を表現
• 低レイヤの例例外処理理、リソース管理理に煩わすことなく、実
テンプレート 装に専念念できる。
35
Copyright © 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 Dataコアエンジニアの来⽇日決定
12/9 18:00〜~21:00
VMware 浜松町オフィス
Spring Dataの神髄を語る
参加希望者は、以下のURLから登録ください。
http://atnd.org/events/34055
Engineer at SpringSource
Oliver Gierke is engineer at SpringSource, a division of VMware,
project lead of the Spring Data JPA module and involved into other
Spring Data modules (e.g. MongoDB) as well. He has been into
developing enterprise applications and open source projects for
over 6 years now. His working focus is centered around software
architecture, Spring and persistence technologies. He is regularly
speaking at German and international conferences as well as
author of technology articles.
Oliver Gierke
36
Copyright © 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 .