Spring Data JPAによるデータアクセス徹底入門 #jsug

10.9K Views

May 26, 22

スライド概要

JSUG Spring Fest 2017での発表資料です。

profile-image

Java、Spring、IntelliJ IDEA

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

1 1JWPUBMೝఆߨࢣ͕‫͑ڭ‬Δʂ 4QSJOH%BUB+1"ʹΑΔ σʔλΞΫηεపఈೖ໳ ‫ ג‬ΧαϨΞϧଟాਅහ +46(4QSJOH'FTU ೥݄೔ ۚ (C) CASAREAL, Inc. All rights reserved.

2.

<࣭໰>03ϚούʔɺԿΛ࢖ͬͯ·͔͢ʁ ᶃ +1"PS)JCFSOBUF ᶄ .Z#BUJT ᶅ 4QSJOH+%#$ ᶆ ͦͷଞ 2 (C) CASAREAL, Inc. All rights reserved.

3.

<࣭໰>ͳͥ+1"Λબͼ·͔ͨ͠ʁ ᶃ ඪ४ٕज़͔ͩΒ ᶄ ੈͷதʹ৘ใ͕ଟ͍͔Β ᶅ ཁ݅ʹద͍͔ͯͨ͠Β ᶆ ͦͷଞ 3 (C) CASAREAL, Inc. All rights reserved.

4.

͜ͷηογϣϯʹ͍ͭͯ ▸ 4QSJOH%BUB+1"ͷ‫ૅج‬஌ࣝΛஸೡʹղઆʂ ▸ +1"੒෼͕ଟΊͰ͢😅 ▸ ຊ೔ͷαϯϓϧίʔυ͸(JU)VCʹ͋Γ·͢ ▸ IUUQTHJUIVCDPN.BTBUPTIJ5BEBTQSJOHEBUB KQBEBJUPLBJ 4 (C) CASAREAL, Inc. All rights reserved.

5.

ࣗ‫঺ݾ‬հ ▸ ଟాਅහʢ!TVLF@NBTBʣ ▸ ‫ݚ‬मτϨʔφʔ!ΧαϨΞϧ ▸ 4QSJOH+BWB&& ▸ 1JWPUBMೝఆߨࢣʢࠃ಺Ͱ͸਺ਓʣ ▸ +46(ελοϑɺ(6(+ӡӦϝϯόʔ 5 (C) CASAREAL, Inc. All rights reserved.

6.

ΧαϨΞϧձࣾ঺հ ▸ ৭Μͳϓϩάϥϛϯά‫ޠݴ‬ͷ‫ݚ‬मΛ΍ͬͯ·͢ʂ 6 (C) CASAREAL, Inc. All rights reserved.

7.

$PSF4QSJOH ▸ 1JWPUBMೝఆτϨʔχϯά ▸ ओཁςΫϊϩδʔΛ໢ཏ ▸ ಺෦ͷ࢓૊Έ·Ͱৄࡉʹֶशʢ%*͚ͩͰ೔ؒʣ ▸ ࣍ճ࣮ࢪ෼͔Β4QSJOHʹରԠʂ ▸ ೥݄೔ʙ೔ɺ೔ʙ೔ͷ‫ܭ‬೔ؒ ▸ IUUQTXXXDBTBSFBMDPKQMTTFSWJDFPQFOTFNJOBSQJWPUBM Q 7 (C) CASAREAL, Inc. All rights reserved.

8.

ͦͷଞͷ4QSJOHؔ࿈τϨʔχϯάҰཡ ‫ݚ‬म໊ ೔਺ Spring Web 4೔ؒ Spring MVCͷ‫͔ૅج‬ΒԠ༻·Ͱ 2೔ؒ Spring Bootͷ‫͔ૅج‬ΒԠ༻·Ͱ Spring Boot Developer Spring Cloud Services 3೔ؒ આ໌ Spring CloudʹΑΔMicroservices ։ൃ 8 (C) CASAREAL, Inc. All rights reserved.

9.

<ࠂ஌>1JWPUBM$MPVE'PVOESZ೔ϋϯζΦϯ ▸ ݄೔ ໦  ▸ 1JWPUBM$MPVE'PVOESZΛར༻ͨ͠ΞϓϦέʔ γϣϯ։ൃΛମ‫͢·͖Ͱݧ‬ʂ ▸ ແྉ ▸ IUUQTXXXDBTBSFBMDPKQMTTFSWJDF FWFOUTFNJOBSMJOFVQGT 9 (C) CASAREAL, Inc. All rights reserved.

10.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 10 (C) CASAREAL, Inc. All rights reserved.

11.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 11 (C) CASAREAL, Inc. All rights reserved.

12.

4QSJOH%BUB+1"ͱ͸ʁ ▸ 4QSJOH%BUB+1"4QSJOH%BUB +1" 12 (C) CASAREAL, Inc. All rights reserved.

13.

4QSJOH%BUBͱ͸ʁ ▸ ༷ʑͳσʔλΞΫηεٕज़Λಉ͡Α͏ ʹѻ͑ΔΑ͏ʹ͢ΔϓϩδΣΫτ ▸ "1*Λந৅Խɾ‫ڞ‬௨Խ ▸ ൃੜ͢Δྫ֎Λ‫ڞ‬௨ྫ֎ʹม‫׵‬ ▸ ϦϙδτϦΠϯλϑΣʔεͰ؆୯ʹॻ͚Δ ▸ +1"ɺ(FNpSFɺ.POHP%#ɺ3FEJTɺ $BTTBOESBͳͲଟ͘ͷٕज़ʹରԠ 13 (C) CASAREAL, Inc. All rights reserved.

14.

+1"ͱ͸ʁ ▸ +BWB&&ͷҰ෦ͱͯ͠ඪ४Խ ͞ΕͨσʔλΞΫηεٕज़ͷ ࢓༷ ▸ ࣮ମ͸+43ͱ͍͏จॻ ▸ ܾ·ͬͯΔͷ͸ΠϯλϑΣʔεɾ Ξϊςʔγϣϯɾྫ֎͕΄ͱΜ Ͳ 14 (C) CASAREAL, Inc. All rights reserved.

15.

+1"࣮૷ϥΠϒϥϦ ࠓճ͸ίϨ ᶃ )JCFSOBUF ▸ 03Ϛούʔͷ૲෼͚తଘࡏ ▸ 4QSJOH%BUB+1"͸‫ج‬ຊతʹίϨ ᶄ &DMJQTF-JOL ▸ +1"ͷࢀর࣮૷ ▸ (MBTT'JTI8FC-PHJDͳͲʹ಺แ ※ଞͷ࣮૷͸JPA 2.1(Java EE 7)ʹ ɹରԠ͍ͯ͠ͳ͍ 15 (C) CASAREAL, Inc. All rights reserved.

16.

+1"ͷಛ௃ ▸ %#੡඼ʹґଘ͠ͳ͍ૢ࡞͕‫ه‬ड़Ͱ͖Δ ▸ ΤϯςΟςΟΛςʔϒϧͱରͰ࡞Δ ▸ ʮ4&-&$5ͷ݁ՌͱରʯͰ͸ͳ͍ͷͰ஫ҙʂ ▸ ରଟͷΑ͏ͳϦϨʔγϣϯΛද‫͖Ͱݱ‬Δ 16 (C) CASAREAL, Inc. All rights reserved.

17.

4QSJOH%BUB+1"Λ࢖͓͏ ▸ 4QSJOH#PPUͷ৔߹ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> 17 (C) CASAREAL, Inc. All rights reserved.

18.

4QSJOH%BUB+1"Λ࢖͓͏ ▸ ඇ4QSJOH#PPUͷ৔߹ <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.2.12.Final</version> </dependency> 18 (C) CASAREAL, Inc. All rights reserved.

19.

42-ϩάͷग़ྗ͸ඞਢʂ ▸ τϥϒϧγϡʔςΟϯάͰॏཁʂ ▸ 4QSJOH#PPUɿBQQMJDBUJPOQSPQFSUJFTʹԼ‫ه‬Λઃఆ logging.level.org.hibernate.SQL=debug logging.level.org.hibernate.type.descriptor.sql.BasicBinder=\ trace ▸ ඇ4QSJOH#PPUɿMPHCBDLYNMʹԼ‫ه‬Λઃఆ <logger name="org.hibernate.SQL" level="DEBUG" /> <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" /> ※hibernate.show_sql=trueͩͱϩάͷϑΥʔϚοτΛࢦఆͰ͖ͳ͍ͷͰ͜ΕΒͷํ๏͕Φεεϝ (C) CASAREAL, Inc. All rights reserved. 19

20.

42-ϩάΛಡΈ΍͘͢͢Δ ▸ վߦ΍Πϯσϯτ͕ೖΔΑ͏ʹͳΔ ▸ 4QSJOH#PPUɿBQQMJDBUJPOQSPQFSUJFTʹԼ‫ه‬Λઃఆ spring.jpa.properties.hibernate.format_sql=true ▸ ඇ4QSJOH#PPUɿ+BWB$POpHʹԼ‫ه‬Λ‫ه‬ड़ ▸ IUUQTHJUIVCDPN.BTBUPTIJ5BEBTQSJOHGPSTQSJOHCPPUCMPCNBTUFS TQSJOHTSDNBJOKBWBDPNFYBNQMFQFSTJTUFODFDPOpH +QB$POpHKBWB- 20 (C) CASAREAL, Inc. All rights reserved.

21.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 21 (C) CASAREAL, Inc. All rights reserved.

22.

+1"ͷΞʔΩςΫνϟ &OUJUZ .BOBHFS 'BDUPSZ ӬଓԽίϯςΩετ ੜ੒ &OUJUZ &OUJUZ &OUJUZ &OUJUZ &OUJUZ .BOBHFS DB ΫϥΠΞϯτ ϓϩάϥϜ 22 (C) CASAREAL, Inc. All rights reserved.

23.

ΤϯςΟςΟ ▸ ‫ج‬ຊతʹςʔϒϧͱ ରͰ࡞Δ ▸ ʮ4&-&$5จͱରʯ Ͱ͸ͳ͍ʂ @Entity public class Product { @Id private Integer id; private String name; // setter/getter } ▸ ओΩʔϑΟʔϧυඞ ਢ 23 (C) CASAREAL, Inc. All rights reserved.

24.

&OUJUZ.BOBHFS ▸ ΤϯςΟςΟͷʮঢ়ଶʯΛૢ࡞͢Δ૭‫ޱ‬ ▸ ʮ$36%ૢ࡞ͷ૭‫ޱ‬ʯͰ͸ͳ͍ͷͰ஫ҙʂ ▸ ‫݁ࡧݕ‬Ռ͸ӬଓԽίϯςΩετʢ‫ޙ‬ड़ʣʹอଘ ▸ Πϯελϯε͸ τϥϯβΫγϣϯ୯Ґ 24 (C) CASAREAL, Inc. All rights reserved.

25.

&OUJUZ.BOBHFS'BDUPSZ ▸ &OUJUZ.BOBHFSΛੜ੒͢Δ ▸ Πϯεϯλϯε͸ΞϓϦέʔγϣϯͰͭͷΈ ʢʹγϯάϧτϯʣ 25 (C) CASAREAL, Inc. All rights reserved.

26.

ӬଓԽίϯςΩετ ▸ ΤϯςΟςΟͷΩϟογϡͷΑ͏ͳ΋ͷ ▸ ΤϯςΟςΟ͕ӬଓԽίϯςΩετʹ‫·ؚ‬Ε͍ͯΔ͔ Ͳ͏͔͕ඇৗʹॏཁ ▸ τϥϯβΫγϣϯ୯ҐͰ ੜ੒ɾഁ‫͞غ‬ΕΔ 26 (C) CASAREAL, Inc. All rights reserved.

27.

ΤϯςΟςΟͷʮঢ়ଶʯ ঢ়ଶ NEW આ໌ new͞Εͨ͹͔Γ MANAGED ӬଓԽίϯςΩετͰ؅ཧ͞Ε͍ͯΔ DETACHED ӬଓԽίϯςΩετ͔Β෼཭͞Ε͍ͯΔ REMOVED ࡟আ͕༧໿͞Ε͍ͯΔ 27 (C) CASAREAL, Inc. All rights reserved.

28.

&OUJUZ.BOBHFSͷϝιου ϝιου આ໌ find() ओΩʔ‫ࡧݕ‬ persist() 1݅ͷӬଓԽ remove() 1݅ͷ࡟আ merge() ӬଓԽίϯςΩετ؅ཧԼʹ͢Δ detach() ӬଓԽίϯςΩετ؅ཧ֎ʹ͢Δ flush() มߋΛDB΁൓ө clear() ӬଓԽίϯςΩετΛΫϦΞ refresh() ΤϯςΟςΟͷ಺༰ΛDB࠷৽ʹ্ॻ͖ 28 (C) CASAREAL, Inc. All rights reserved.

29.

˒ॏཁ˒ΤϯςΟςΟͷঢ়ଶભҠ ӬଓԽίϯςΩετ persist() /&8 ঢ়ଶ detach() clear() %&5"$)&% ঢ়ଶ ."/"(&% ঢ়ଶ remove() merge() detach() clear() 3&.07&% ঢ়ଶ find() JPQL flush() refresh() flush() 29 (C) CASAREAL, Inc. All rights reserved.

30.

ओΩʔ‫ࡧݕ‬ ▸ ‫ج‬ຊతʹશϑΟʔϧυʹ૬౰͢Δྻ͕‫͞ࡧݕ‬ΕΔ EntityManager em = …; // ୈ1Ҿ਺ʹΤϯςΟςΟΫϥεɺୈ2Ҿ਺ʹओΩʔ஋ Product product = em.find(Product.class, 1); select product0_.id as id1_5_0_, product0_.category_id as category4_5_0_, product0_.name as name2_5_0_, product0_.price as price3_5_0_, product0_.vendor_id as vendor_i5_5_0_ from product product0_ where product0_.id=? (C) CASAREAL, Inc. All rights reserved. ͜ΜͳSQL͕ ൃߦ͞ΕΔ 30

31.

௥Ճ ▸ ‫ج‬ຊతʹશϑΟʔϧυ͕*/4&35͞ΕΔ ▸ */4&35͠ͳ͍ϑΟʔϧυʹ͸!5SBOTJFOUΛ෇Ճ͢Δ Product product = new Product(“Ε͍ͧ͏͜”, 100000L); em.persist(product); insert into product (category_id, name, price, vendor_id, id) values (?, ?, ?, ?, ?) 31 (C) CASAREAL, Inc. All rights reserved.

32.

ߋ৽ ▸ ."/"(&%ঢ়ଶͷΤϯςΟςΟʹTFUUFSͰ஋Λ ߋ৽ˠ൓ө // find()Ͱ‫͢ࡧݕ‬ΔͱMANAGEDঢ়ଶ Product product = em.find(Product.class, 1); // ஋ͷߋ৽ product.setName("ߋ৽‫ޙ‬ͷ໊લ"); // ߋ৽ͷ൓ө(ίϛοτͰ΋OK) em.flush(); 32 (C) CASAREAL, Inc. All rights reserved.

33.

ߋ৽ select … from product product0_ where product0_.id=? binding parameter [1] as [INTEGER] - [1] update product set category_id=?, name=?, price=?, vendor_id=? where id=? binding parameter [1] as [INTEGER] - [1] binding parameter [2] as [VARCHAR] - [ߋ৽‫ޙ‬ͷ໊લ] binding parameter [3] as [BIGINT] - [100000] binding parameter [4] as [INTEGER] - [1] binding parameter [5] as [INTEGER] - [1] 33 (C) CASAREAL, Inc. All rights reserved.

34.

NFSHF ʹΑΔߋ৽ ▸ OFXͨ͠ΤϯςΟςΟΛNFSHF ͢Δ Product product = new Product(); product.setId(1); product.setName(“ߋ৽‫ޙ‬ͷ໊લ"); // merge()Ͱ΋ߋ৽Ͱ͖Δ em.merge(product); em.flush(); 34 (C) CASAREAL, Inc. All rights reserved.

35.

NFSHF ʹΑΔߋ৽ ▸ 4&-&$5จʴ61%"5&จ͕ൃߦ͞ΕΔ select … from product product0_ where product0_.id=? binding parameter [1] as [INTEGER] - [1] update product set category_id=?, name=?, price=?, vendor_id=? where id=? binding parameter [1] as [INTEGER] - [null] binding parameter [2] as [VARCHAR] - [ߋ৽‫ޙ‬ͷ໊લ] binding parameter [3] as [BIGINT] - [null] binding parameter [4] as [INTEGER] - [null] binding parameter [5] as [INTEGER] - [1] 35 (C) CASAREAL, Inc. All rights reserved.

36.

NFSHF ͷམͱ݀͠ ▸ Ҿ਺ΤϯςΟςΟͷίϐʔ͕࡞ΒΕɺͦΕ͕໭ Γ஋͔ͭ."/"(&%ঢ়ଶʹͳΔ ▸ Ҿ਺ࣗମ͸."/"(&%ঢ়ଶʹͳΒͳ͍ͷͰ஫ҙ Product mergedProduct = em.merge(product); // ໭Γ஋͸MANAGEDঢ়ଶ assertTrue(em.contains(mergedProduct)); // Ҿ਺ͷঢ়ଶ͸มΘΒͳ͍ assertFalse(em.contains(product)); 36 (C) CASAREAL, Inc. All rights reserved.

37.

࡟আ ▸ ."/"(&%ঢ়ଶͷΤϯςΟςΟΛSFNPWF ͢Δ // find()Ͱ‫͢ࡧݕ‬ΔͱMANAGEDঢ়ଶ Product product = em.find(Product.class, 30); // REMOVEDঢ়ଶʹ͢Δ em.remove(product); // ߋ৽ͷ൓ө(ίϛοτͰ΋OK) em.flush(); 37 (C) CASAREAL, Inc. All rights reserved.

38.

࡟আ select … from product product0_ where product0_.id=? binding parameter [1] as [INTEGER] - [30] delete from product where id=? binding parameter [1] as [INTEGER] - [30] 38 (C) CASAREAL, Inc. All rights reserved.

39.

࡟আͷΩϟϯηϧ ▸ EFUBDI ·ͨ͸DMFBS Ͱ%&5"$)&%ঢ়ଶʹ͢Δ // find()Ͱ‫͢ࡧݕ‬ΔͱMANAGEDঢ়ଶ Product product = em.find(Product.class, 30); // REMOVEDঢ়ଶʹ͢Δ em.remove(product); // DETACHEDঢ়ଶʹ͢Δ em.detach(product); // DETACHEDঢ়ଶͳͷͰDBʹ൓ө͞Εͳ͍ em.flush(); 39 (C) CASAREAL, Inc. All rights reserved.

40.

·ͱΊ ▸ ΤϯςΟςΟͷʮঢ়ଶʯʹ஫ҙ ▸ pOE ͰओΩʔ‫ࡧݕ‬ ▸ QFSTJTU Ͱ௥Ճ ▸ ߋ৽͸pOE  TFUUFSͰߋ৽ ▸ SFNPWF Ͱ࡟আ 40 (C) CASAREAL, Inc. All rights reserved.

41.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 41 (C) CASAREAL, Inc. All rights reserved.

42.

+12-ͱ͸ʁ ▸ 42-ϥΠΫͳจ๏Ͱ4&-&$561%"5&%&-&5& Λ‫ه‬ड़͢Δ ▸ */4&35͸ແ͍ ▸ %#੡඼ʹґଘ͠ͳ͍ΫΤϦ͕‫ه‬ड़Մೳ ▸ %#‫ݻ‬༗‫ػ‬ೳʹ͍ͭͯ͸‫ޙ‬ड़ 42 (C) CASAREAL, Inc. All rights reserved.

43.
[beta]
‫ج‬ຊతͳ࢖͍ํ
▸ ୯Ұ‫ࡧݕ‬
TypedQuery<Product> query = em.createQuery(
"SELECT p FROM Product p WHERE p.id = :id",
Product.class);
query.setParameter("id", 1);
Product product = query.getSingleResult();

▸ ෳ਺‫ࡧݕ‬
TypedQuery<Product> query = em.createQuery(
"SELECT p FROM Product p WHERE p.id <= :maxId"
+ " ORDER BY p.id", Product.class);
query.setParameter("maxId", 5);
List<Product> list = query.getResultList();
43
(C) CASAREAL, Inc. All rights reserved.

44.

࣮ߦ͢Δͱ42-ʹ຋༁͞ΕΔ ▸ ͲΜͳ42-͕ൃߦ͞ΕΔ͔͸+1"࢓༷Ͱະఆٛ ʹ‫׬‬શʹ࣮૷ґଘʢԼ‫ه‬͸)JCFSOBUFͷྫʣ select product0_.id as id1_5_, product0_.category_id as category4_5_, product0_.name as name2_5_, product0_.price as price3_5_, product0_.vendor_id as vendor_i5_5_ from product product0_ where product0_.id=? (C) CASAREAL, Inc. All rights reserved. 44

45.

+12-ͷ‫ج‬ຊจ๏ SELECT ࣝผม਺ FROM ΤϯςΟςΟ໊ [AS] ࣝผม਺ WHERE ࣝผม਺.ϓϩύςΟ໊ <= :ύϥϝʔλ໊ ▸ ࣝผม਺ʹΤϯςΟςΟͷผ໊ʢඞਢʣ ▸ ΤϯςΟςΟ໊΍ϓϩύςΟ໊͸ɺ େจࣈখจࣈΛ‫۠ʹີݫ‬ผ͢ΔͷͰ஫ҙʂ ▸ 4&-&$5౳͸۠ผ͠ͳ͍͕େจࣈͷํ͕ؒҧ͑ʹ͍͘ 45 (C) CASAREAL, Inc. All rights reserved.

46.

ίϯετϥΫλࣜ ▸ ඇΤϯςΟςΟͳΫϥεʹ΋ϚοϐϯάՄೳ ▸ ओͳ༻్͸ू‫ܭ‬ SELECT new com.example.demo.dto.CountDto( od.product.id, COUNT(od)) FROM OrderDetail od GROUP BY od.product.id ύοέʔδ໊෇͖ͳͷ͕ਏ͍ʜ 46 (C) CASAREAL, Inc. All rights reserved.

47.

ͦͷଞͷ+12-Ͱग़དྷΔ͜ͱᶃ ▸ ݁߹ʢ*//&3+0*/ɺ-&'5065&3+0*/ɻৄࡉ͸‫ޙ‬ ड़ʣ ▸ #&58&&/ɺ*/ɺ-*,&ɺ&9*454ɺ"--ɺ"/:ɺ$"4& ▸ (3061#:ɺ)"7*/( ▸ ू‫਺ؔܭ‬ʢ."9 ɺ.*/ ɺ$06/5 ɺ46. ɺ "7( ͷͭʣ ▸ ෭໰߹ͤʢ8)&3&۟ͱ)"7*/(۟ͰͷΈʣ 47 (C) CASAREAL, Inc. All rights reserved.

48.

ͦͷଞͷ+12-Ͱग़དྷΔ͜ͱᶄ ▸ จࣈྻؔ਺ʢ$0/$"5 ɺ46#453*/( ɺ53*. ɺ -08&3 ɺ611&3 ɺ-&/(5) ɺ-0$"5& ʣ ▸ ࢉज़ؔ਺ʢ"#4 ɺ4235 ɺ.0% ɺ4*;& ɺ */%&9 ʣ ▸ ೔࣌ؔ਺ʢ$633&/5@%"5&ɺ$633&/5@5*.&ɺ $633&/5@5*.&45".1ʣ ▸ %#‫ݻ‬༗ؔ਺ͷ‫ͼݺ‬ग़͠ʢ‫ޙ‬ड़ʣ 48 (C) CASAREAL, Inc. All rights reserved.

49.

+12-Ͱग़དྷͳ͍͜ͱ ▸ */4&35 ▸ ू߹ԋࢉʢ6/*0/ɺ6/*0/"--ɺ */5&34&$5ɺ&9$&15ʣ ▸ '30.۟Ͱͷ෭໰߹ͤ 49 (C) CASAREAL, Inc. All rights reserved.

50.

·ͱΊ ▸ 42-ϥΠΫͳจ๏Ͱ%#ʹґଘ͠ͳ͍ΫΤϦΛ‫ه‬ ड़Մೳ ▸ Ͱ͖Δ͜ͱɾͰ͖ͳ͍͜ͱΛ͔ͬ͠Γҙࣝ͢΂ ͠ 50 (C) CASAREAL, Inc. All rights reserved.

51.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 51 (C) CASAREAL, Inc. All rights reserved.

52.
[beta]
ϦϨʔγϣϯΛද‫͢ݱ‬Δ
▸ !0OF5P0OF
▸ !0OF5P.BOZ
▸ !.BOZ5P0OF
▸ !.BOZ5P.BOZ

@Entity
public class Product {
…
@ManyToOne
@JoinColumn(name="vendor_id")
private Vendor vendor;
}
@Entity
public class Vendor {
…
}

52
(C) CASAREAL, Inc. All rights reserved.

53.

ϑΣονͱ͸ ▸ ͋ΔΤϯςΟςΟΛಡΈࠐΜͩ࣌ɺϦϨʔγϣϯઌ ͷΤϯςΟςΟΛ͍ͭಡΈࠐΉ͔ ᶃ &"(&3ϑΣονɿଈ࣌ಡΈࠐΈ ▸ ಉ࣌ʹಡΈࠐΉʢ)JCFSOBUFͩͱ+0*/ɺ&DMJQTF-JOLͩͱ 4&-&$5จ͕ෳ਺ಉ࣌ʹൃߦʣ ᶄ -";:ϑΣονɿ஗ԆಡΈࠐΈ ▸ ඞཁʹͳͬͨ࣌ʢHFUUFS͕‫ݺ‬͹Εͨ࣌ʣʹಡΈࠐΉ 53 (C) CASAREAL, Inc. All rights reserved.

54.
[beta]
ϑΣονͷࢦఆํ๏
▸ !9YY5P9YYͷGFUDIଐੑʹࢦఆ͢Δ
▸ !9YY5P.BOZˠσϑΥϧτͰ-";:
▸ !9YY5P0OFˠσϑΥϧτͰ&"(&3
@Entity
public class Product {
…
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="vendor_id")
private Vendor vendor;
}
54
(C) CASAREAL, Inc. All rights reserved.

55.
[beta]
-";:ϑΣονͷྫ
@Entity public class Product {
@ManyToOne(fetch = FetchType.LAZY)
private Vendor vendor;
}
Product p = em.find(Product.class, 1);
Vendor v = p.getVendor();
String n = v.getName() // ͜͜Ͱ΋SELECT

2ճ
SELECT

select (ྻলུ) from product product0_
where product0_.id=?
select (ྻলུ) from vendor vendor0_
where vendor0_.id=?
55
(C) CASAREAL, Inc. All rights reserved.

56.

-";:ϑΣονͷ஫ҙ఺ ▸ ӬଓԽίϯςΩετഁ‫ޙغ‬ʢ㲈τϥϯβΫγϣϯ ऴྃ‫ޙ‬ʣʹϑΣον͢Δͱྫ֎͕ൃੜ͢Δ em.getTransaction().begin(); Product p = em.find(Product.class, 1); em.getTransaction().rollback(); Vendor v = p.getVendor(); String n = v.getName(); // ͜͜Ͱྫ֎ൃੜ 56 (C) CASAREAL, Inc. All rights reserved.

57.

&"(&3ϑΣονͷྫ @Entity public class Product { @ManyToOne(fetch = FetchType.EAGER) private Vendor vendor; } 1ճͷΈ SELECT Product p = em.find(Product.class, 1); Vendor v = p.getVendor(); // ‫ʹط‬ಡΈࠐΈࡁΈ select (ྻলུ) from product product0_ left outer join vendor vendor2_ on product0_.vendor_id=vendor2_.id where product0_.id=? 57 (C) CASAREAL, Inc. All rights reserved.

58.
[beta]
ϑΣον͸‫ج‬ຊ-";:ʂ
▸ &"(&3ʹ͢Δͱෆཁͳσʔλ·ͰಡΈࠐΉ͔Β
▸ !0OF5P.BOZͰେྔͷσʔλΛಡΈࠐΉͱ͔‫ڪ‬ා
ʢʹ/ ໰୊ʣ

▸ -";:͔Β&"(&3ʹಈతʹมߋ͍ͨ͠৔߹͸ɺ
+12-ͷ+0*/'&5$)จΛར༻͢Δ

58
(C) CASAREAL, Inc. All rights reserved.

59.
[beta]
/ ໰୊͕‫͜ى‬Δྫ
// order_summary΁ͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ
List<OrderSummary> list = em.createQuery(
"SELECT os FROM OrderSummary os",OrderSummary.class)
.getResultList();
for (OrderSummary os : list) {
// ·ͩorder_detail͸ಡΈࠐ·Ε͍ͯͳ͍
System.out.println(os);
for (OrderDetail od : os.getOrderDetailList()) {
// ͜͜Ͱorder_detail΁ͷSELECT͕ൃߦ͞ΕΔ(Nճ)
System.out.println(od);
}
}
59
(C) CASAREAL, Inc. All rights reserved.

60.
[beta]
୯ͳΔ+0*/͸ҙຯ͕ͳ͍
// order_summary΁ͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ
List<OrderSummary> list = em.createQuery(
"SELECT os FROM OrderSummary os" +
" JOIN os.orderDetailList", OrderSummary.class)
.getResultList();
for (OrderSummary os : list) {
// ·ͩorder_detail͸ಡΈࠐ·Ε͍ͯͳ͍
System.out.println(os);
for (OrderDetail od : os.getOrderDetailList()) {
// ͜͜Ͱorder_detail΁ͷSELECT͕ൃߦ͞ΕΔ(Nճ)
System.out.println(od);
}
}
(C) CASAREAL, Inc. All rights reserved.

60

61.

୯ͳΔ+0*/͸ҙຯ͕ͳ͍ select ordersumma0_.id as id1_3_, ordersumma0_.customer_id as customer3_3_, ordersumma0_.order_timestamp as order_ti2_3_ from order_summary ordersumma0_ inner join order_detail orderdetai1_ on ordersumma0_.id=orderdetai1_.summary_id where ordersumma0_.id=? JOINͯ͠Δ͚Ͳ order_summaryͷྻ ͔͠औಘͯ͠ͳ͍ 61 (C) CASAREAL, Inc. All rights reserved.

62.
[beta]
+0*/'&5$)จͰղܾʂʁ
▸ -";:ʹͳ͍ͬͯΔϦϨʔγϣϯΛಈతʹ&"(&3
ʹมߋͯ͘͠ΕΔ
// order_summary΁ͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ
List<OrderSummary> list = em.createQuery(
"SELECT os FROM OrderSummary os" +
" JOIN FETCH os.orderDetailList" +
, OrderSummary.class)
.getResultList();

62
(C) CASAREAL, Inc. All rights reserved.

63.

+0*/'&5$)จͰղܾʂʁ select ordersumma0_.id as id1_3_0_, orderdetai1_.id as id1_2_1_, ordersumma0_.customer_id as customer3_3_0_, ordersumma0_.order_timestamp as order_ti2_3_0_, orderdetai1_.amount as amount2_2_1_, orderdetai1_.product_id as product_3_2_1_, orderdetai1_.summary_id as summary_4_2_0__, orderdetai1_.id as id1_2_0__ from order_summary ordersumma0_ inner join order_detail orderdetai1_ on ordersumma0_.id=orderdetai1_.summary_id where ordersumma0_.id=? JOINͯ͠ શྻऔಘ 63 (C) CASAREAL, Inc. All rights reserved.

64.

දࣔ͢Δͱɾɾɾ OrderSummary{id=1, orderTimestamp=2017-04-01T10:00} OrderDetail{id=1, amount=2} OrderDetail{id=2, amount=2} OrderDetail{id=3, amount=2} OrderSummary{id=1, orderTimestamp=2017-04-01T10:00} OrderDetail{id=1, amount=2} OrderDetail{id=2, amount=2} OrderDetail{id=3, amount=2} OrderSummary{id=1, orderTimestamp=2017-04-01T10:00} OrderDetail{id=1, amount=2} OrderDetail{id=2, amount=2} OrderDetail{id=3, amount=2} OrderSummary{id=2, orderTimestamp=2017-04-01T10:00} OrderDetail{id=4, amount=2} OrderDetail{id=5, amount=2} ɾɾɾ ಉ͡σʔλ͕ Կճ΋ʂʁ 64 (C) CASAREAL, Inc. All rights reserved.

65.
[beta]
+0*/'&5$) %*45*/$5Ͱղܾʂ
// order_summary΁ͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ
List<OrderSummary> list = em.createQuery(
"SELECT DISTINCT os FROM OrderSummary os" +
" JOIN FETCH os.orderDetailList" +
, OrderSummary.class)
.getResultList();

65
(C) CASAREAL, Inc. All rights reserved.

66.

+0*/'&5$) %*45*/$5Ͱղܾʂ select distinct ordersumma0_.id as id1_3_0_, distinct͕෇͕͘ orderdetai1_.id as id1_2_1_, ordersumma0_.customer_id as customer3_3_0_, SQLͷ݁Ռ͸ ordersumma0_.order_timestamp as order_ti2_3_0_, orderdetai1_.amount as amount2_2_1_, ͜ͷ৔߹͸ಉ͡ orderdetai1_.product_id as product_3_2_1_, orderdetai1_.summary_id as summary_4_2_0__, orderdetai1_.id as id1_2_0__ from order_summary ordersumma0_ inner join order_detail orderdetai1_ on ordersumma0_.id=orderdetai1_.summary_id where ordersumma0_.id=? 66 (C) CASAREAL, Inc. All rights reserved.

67.

දࣔ͢Δͱɾɾɾ OrderSummary{id=1, orderTimestamp=2017-04-01T10:00} OrderDetail{id=3, amount=2} OrderDetail{id=1, amount=2} OrderDetail{id=2, amount=2} OrderSummary{id=2, orderTimestamp=2017-04-01T10:00} OrderDetail{id=5, amount=2} OrderDetail{id=6, amount=2} OrderDetail{id=4, amount=2} OrderSummary{id=9, orderTimestamp=2017-04-01T10:00} OrderSummary{id=7, orderTimestamp=2017-04-01T10:00} OrderSummary{id=6, orderTimestamp=2017-04-01T10:00} OrderSummary{id=5, orderTimestamp=2017-04-01T10:00} OrderSummary{id=4, orderTimestamp=2017-04-01T10:00} OrderSummary{id=3, orderTimestamp=2017-04-01T10:00} OrderSummary{id=8, orderTimestamp=2017-04-01T10:00} OrderSummary{id=10, orderTimestamp=2017-04-01T10:00} σʔλͷॏෳ͕ ແ͘ͳͬͨʂ 67 (C) CASAREAL, Inc. All rights reserved.

68.

+12-ͷ%*45*/$5۟ ▸ 42-ͷ%*45*/$5۟ʴ-JTUͷॏෳ΋࡟আ͢Δ 68 (C) CASAREAL, Inc. All rights reserved.

69.

ωετͨ͠+0*/'&5$) ▸ )JCFSOBUFͰͷΈΤΠϦΞεΛ෇͚Δ͜ͱ͕ग़དྷ Δ // order_summaryɺorder_detailɺproduct͕JOIN͞ΕΔ OrderSummary orderSummary = em.createQuery( "SELECT os FROM OrderSummary os" + " JOIN FETCH os.orderDetailList od" + ΤΠϦΞε " JOIN FETCH od.product p" + " WHERE os.id = :id", OrderSummary.class) .setParameter("id", 1) .getSingleResult(); 69 (C) CASAREAL, Inc. All rights reserved.

70.

ωετͨ͠+0*/'&5$) select ordersumma0_.id as id1_3_0_, orderdetai1_.id as id1_2_1_, product2_.id as id1_4_2_, ordersumma0_.customer_id as customer3_3_0_, ordersumma0_.order_timestamp as order_ti2_3_0_, orderdetai1_.amount as amount2_2_1_, orderdetai1_.product_id as product_3_2_1_, orderdetai1_.summary_id as summary_4_2_0__, orderdetai1_.id as id1_2_0__, product2_.category_id as category4_4_2_, product2_.name as name2_4_2_, product2_.price as price3_4_2_, product2_.vendor_id as vendor_i5_4_2_ from order_summary ordersumma0_ inner join order_detail orderdetai1_ on ordersumma0_.id=orderdetai1_.summary_id inner join product product2_ on orderdetai1_.product_id=product2_.id where ordersumma0_.id=? 3ͭͷදΛ JOINͯ͠ શྻऔಘ 70 (C) CASAREAL, Inc. All rights reserved.

71.
[beta]
·ͱΊ
▸ 'FUDI5ZQF͸-";:Λࢦఆ͠Α͏ʂ
▸ +0*/'&5$)จ %*45*/$5۟Ͱ/ ໰୊Λղ
ܾͰ͖Δʂ
▸ ωετͨ͠+0*/'&5$)͸)JCFSOBUFͷΈʂ

71
(C) CASAREAL, Inc. All rights reserved.

72.

<খ‫>ࢭٳ‬ઈࢍɺࣾһืूதʂ ▸ ޷͖ͳٕज़Λੈʹ޿Ί͍ͨ ਓʂ ▸ 4QSJOHɺJ04‫ืٸ‬ ▸ ։ൃҎ֎ͷखஈͰ ٕज़ʹؔΘΓ͍ͨਓʂ ▸ ຖ೔ఆ࣌Ͱ‫ؼ‬Γ͍ͨਓʂ https://www.casareal.co.jp/recruit/jobs/ ls_teacher.php ▸ ϗϫΠτ‫͢Ͱۀا‬ 72 (C) CASAREAL, Inc. All rights reserved.

73.

<খ‫>ࢭٳ‬೥౓৽ਓ‫ݚ‬मʂ https://www.casareal.co.jp/ls/service/shinjinseminar/course01 ▸ Ϟμϯͳٕज़ ✕Ϟμϯͳ‫ڥ؀‬ ✕ΞδϟΠϧ ▸ 3FBDU 4QSJOH#PPU (JU$*5%% Ϋϥ΢υ %PDLFS 73 (C) CASAREAL, Inc. All rights reserved.

74.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 74 (C) CASAREAL, Inc. All rights reserved.

75.

+1"Ͱར༻Ͱ͖Δ%#‫ݻ‬༗‫ػ‬ೳ ▸ ωΠςΟϒ42- ▸ +12-͔Βͷ%#‫ݻ‬༗ؔ਺‫ͼݺ‬ग़͠ ▸ ετΞυϓϩγʔδϟ ▸ ΠϯσοΫε 75 (C) CASAREAL, Inc. All rights reserved.

76.

ωΠςΟϒ42▸ Ϛοϐϯάઌ͸ඞͣΤϯςΟςΟΫϥε ▸ ‫ج‬ຊతʹશྻͷ4&-&$5͕ඞཁ ▸ δΣωϦΫε͕࢖͑ͳ͍ Query query = em.createNativeQuery( "select id, name, price, vendor_id" + " from product where id = :id", Product.class); query.setParameter("id", 1); Product product = (Product) query.getSingleResult(); 76 (C) CASAREAL, Inc. All rights reserved.

77.
[beta]
ΤϯςΟςΟʹϚοϐϯά͠ͳ͍ํ๏
▸ ໭Γ஋Λ0CKFDU<>ʹ͢Δ
Query query = em.createNativeQuery(
"select id, name from product where id = :id");
query.setParameter("id", 1);
// 0൪໨͕idɺ1൪໨͕name
Object[] objs = (Object[]) query.getSingleResult();
Integer productId = (Integer) objs[0];
String productName = (String) objs[1];

Ωϟετ͕ඞཁ
77
(C) CASAREAL, Inc. All rights reserved.

78.

!4RM3FTVMU4FU.BQQJOH ▸ ωΠςΟϒ42-ͷ݁ՌΛ೚ҙͷΫϥεʹϚοϐ ϯάग़དྷΔ ▸ ͨͩ͠ɺ‫ه‬ड़͸͔ͳΓ৑௕ʢ࣍ϖʔδʣ 78 (C) CASAREAL, Inc. All rights reserved.

79.

!4RM3FTVMU4FU.BQQJOH @Entity @SqlResultSetMapping( name = "product_id_name", // ‫ͼݺͰޙ‬ग़࣌͢ͷ໊લ classes = { @ConstructorResult(targetClass = ProductDto.class, columns = { @ColumnResult(name = "id"), @ColumnResult(name = "name") } ΤϯςΟςΟΫϥεʹ ) } ෇Ճ͢Δ͜ͱʹ஫ҙ ) public class Product { … } ※@SqlResultSetMappingsΛར༻͢Ε͹ɺෳ਺ͷ@SqlResultSetMappingΛࢦఆՄೳ (C) CASAREAL, Inc. All rights reserved. 79

80.
[beta]
!4RM3FTVMU4FU.BQQJOH
public class ProductDto {
ΤϯςΟςΟͰͳ͍
private Integer id;
private String name;
೚ҙͷΫϥε
public ProductDto(Integer id, String name) { … }
}
Query query = em.createNativeQuery(
"select id, name from product where id = :id",
"product_id_name"); // @SqlResultSetMappingͷnameଐੑ
query.setParameter("id", 1);
ProductDto productDto =
(ProductDto) query.getSingleResult();

80
(C) CASAREAL, Inc. All rights reserved.

81.
[beta]
+12-͔Βͷ%#‫ݻ‬༗ؔ਺‫ͼݺ‬ग़͠
▸ '6/$5*0/ ؔ਺໊ Ҿ਺ Ҿ਺ ʜ
// H2 DatabaseͷLEFTؔ਺Λར༻
TypedQuery<Product> query = em.createQuery(
"SELECT p FROM Product p"
+ " WHERE FUNCTION('LEFT', p.name, 2) = 'ύϚ'",
Product.class);
List<Product> productList = query.getResultList();

81
(C) CASAREAL, Inc. All rights reserved.

82.

ετΞυϓϩγʔδϟ ▸ DSFBUF4UPSFE1SPDFEVSF2VFSZ StoredProcedureQuery query = em.createStoredProcedureQuery("ϓϩγʔδϟ໊", ΤϯςΟςΟ.class); query.setParameter("ύϥϝʔλ໊", ύϥϝʔλ஋); List<ΤϯςΟςΟ> list = (List<ΤϯςΟςΟ>) query.getResultList(); ※ετΞυϓϩγʔδϟʹৄ͘͠ͳ͍ͨΊචऀ͸ະ‫͢Ͱূݕ‬ɻͥͻࢼͯ͠ϑΟʔυόοΫ͍ͩ͘͞ʂ 82 (C) CASAREAL, Inc. All rights reserved.

83.

ΠϯσοΫε ▸ !*OEFY @Entity @Index(members = {"name"}) public class Product { … } ※ΠϯσοΫεʹৄ͘͠ͳ͍ͨΊචऀ͸ະ‫͢Ͱূݕ‬ɻͥͻࢼͯ͠ϑΟʔυόοΫ͍ͩ͘͞ʂ ※@IndicesΛར༻ͯ͠ෳ਺ͷ@IndexΛࢦఆՄೳ 83 (C) CASAREAL, Inc. All rights reserved.

84.

%#‫ݻ‬༗‫ػ‬ೳͷར༻͸࠷ऴखஈ ▸ ʮ%#੡඼͕มΘͬͯ΋ίʔυͷมߋ͕ඞཁͳ͍ʯ ͱ͍͏+1"ͷϝϦοτ͕ࣦΘΕΔ ▸ ར༻ͤ͟ΔΛಘͳ͍Օॴ͸ɺυΩϡϝϯτͱ͔ ʹ࢒͓͍ͯͨ͠΄͏͕Α͍ ▸ ͦ͏͠ͳ͍ͱ਺೥‫ʹޙ‬%#ΛҠ২͢Δ͜ͱʹͳͬͨͱ ͖େม 84 (C) CASAREAL, Inc. All rights reserved.

85.

·ͱΊ ▸ %#‫ݻ‬༗‫ػ‬ೳ΋ͦͦ͜͜࢖͑Δ ▸ ͨͩ͠ɺ΄Ͳ΄Ͳʹ͠·͠ΐ͏ 85 (C) CASAREAL, Inc. All rights reserved.

86.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 86 (C) CASAREAL, Inc. All rights reserved.

87.

4QSJOH%BUB+1"ͷԼ४උ ▸ -PDBM$POUBJOFS&OUJUZ.BOBHFS'BDUPSZ#FBO Λ#FBOఆٛ ▸ &OUJUZ.BOBHFS'BDUPSZ͕ࣗಈͰ#FBOͱͳΔ ▸ 4QSJOH#PPUͰ͸ఆٛࡁΈ ▸ ৄࡉ͸੿ஶʮ4QSJOHGPS4QSJOH#PPUʯ΁ ▸ IUUQTTQFBLFSEFDLDPNNBTBUPTIJUBEBTQSJOHGPS TQSJOHCPPUOVNCFSKTVH 87 (C) CASAREAL, Inc. All rights reserved.

88.

4QSJOH%BUB+1"ͷศར‫ػ‬ೳ ▸ 3FQPTJUPSZΠϯλϑΣʔε࡞Δ͚ͩ ▸ ϝιου໊͔Βͷ+12-ࣗಈੜ੒ ▸ ‫ڞ‬௨ඇνΣοΫྫ֎΁ͷม‫׵‬ ▸ ‫ࠪ؂‬ରԠ ▸ %BUFBOE5JNF"1* +43 ରԠ 88 (C) CASAREAL, Inc. All rights reserved.

89.

3FQPTJUPSZΠϯλϑΣʔε ▸ +QB3FQPTJUPSZΛ‫ܧ‬ঝͨ͠ΠϯλϑΣʔεΛ࡞ Δ͚ͩͰɺ‫ج‬ຊతͳ$36%ॲཧ͸࡞੒‫ྃ׬‬ public interface ProductRepository extends JpaRepository<Product, Integer> { // த͸ۭͰOK } ※@RepositoryΞϊςʔγϣϯ͸ඞཁ͋Γ·ͤΜ 89 (C) CASAREAL, Inc. All rights reserved.

90.

+QB3FQPTJUPSZͷओͳϝιου ▸ HFU0OF JE pOE#Z*E JE pOE"MM pOE"MM TPSU  pOE"MM JET pOE"MM QBHFBCMF  ▸ TBWF FOUJUZ ˞௥Ճ΋ߋ৽΋Մೳ ▸ EFMFUF#Z*E JE EFMFUF FOUJUZ  ▸ DPVOU FYJTUT#Z*E JE 4QSJOH%BUBͰϝιου໊͕ େ෯ʹมߋ͞ΕͨͷͰ஫ҙʂ 90 (C) CASAREAL, Inc. All rights reserved.

91.

3FQPTJUPSZΠϯλϑΣʔεͷར༻ @Service public class ProductService { private final ProductRepository repo; @Autowired // ϓϩΩγ͕࡞ΒΕͯDI͞ΕΔ public ProductService(ProductRepository repo) { this.repo = repo; } } @Transactional public void insert(Product product) { repo.save(product); } ※@AutowiredΞϊςʔγϣϯ͸Spring 4.3Ҏ߱Ͱ͸লུՄೳ ʢίϯετϥΫλ͕1͚ͭͩͷ৔߹ͷΈʣ (C) CASAREAL, Inc. All rights reserved. 91

92.
[beta]
!2VFSZ
▸ 3FQPTJUPSZΠϯλϑΣʔεʹɺ೚ҙͷ+12-Λ࣮
ߦ͢ΔϝιουΛ௥ՃͰ͖Δ
▸ OBUJWF2VFSZUSVFͱ͢Ε͹ωΠςΟϒ42-΋ॻ͚Δ
public interface ProductRepository
extends JpaRepository<Product, Integer> {
@Query("SELECT p FROM Product p JOIN FETCH p.vendor"
+ " WHERE p.id = :id")
Product findByIdJoinFetch(@Param("id") Integer id);
}
92
(C) CASAREAL, Inc. All rights reserved.

93.
[beta]
!.PEJGZJOH
▸ !2VFSZͰ61%"5&จ΍%&-&5&จΛࢦఆͨ͠
৔߹͸ඞཁ
public interface ProductRepository
extends JpaRepository<Product, Integer> {
@Modifying
@Query("UPDATE Product p SET p.name = :name"
+ " WHERE p.id = :id")
void updateName(@Param("name") String name,
@Param("id") Integer id);
}
93
(C) CASAREAL, Inc. All rights reserved.

94.
[beta]
!-PDL
▸ -PDL.PEF5ZQFΛࢦఆָͯ͠‫؍‬తϩοΫɾ൵‫؍‬
తϩοΫΛࢦఆՄೳ
public interface ProductRepository
extends JpaRepository<Product, Integer> {
@Lock(LockModeType.PESSIMISTIC_READ)
@Query("SELECT p FROM Product p JOIN FETCH p.vendor"
+ " WHERE p.id = :id")
Product findByIdJoinFetch(@Param("id") Integer id);
}
※ϩοΫͷৄࡉ͸Լ‫ࢀه‬র
https://www.slideshare.net/masatoshitada7/jpa20140518-ccc-r15-jjug-ccc
(C) CASAREAL, Inc. All rights reserved.

94

95.
[beta]
!&OUJUZ(SBQI
▸ &OUJUZ(SBQIΛར༻ͨ͠ಈతͳϑΣονͷมߋ
͕Մೳ
public interface ProductRepository
extends JpaRepository<Product, Integer> {
@EntityGraph("άϥϑ໊")
@Query("SELECT p FROM Product p JOIN FETCH p.vendor"
+ " WHERE p.id = :id")
Product findByIdJoinFetch(@Param("id") Integer id);
}
※ΤϯςΟςΟάϥϑͷৄࡉ͸Լ‫ࢀه‬র
https://blog.ik.am/entries/350

95
(C) CASAREAL, Inc. All rights reserved.

96.
[beta]
ϝιου໊͔Βͷ+12-ࣗಈੜ੒
▸ ϧʔϧʹैͬͯϝιου໊Λ‫ه‬ड़͢Ε͹ɺ
!2VFSZͰ+12-Λ‫ه‬ड़͢Δඞཁͳ͠
public interface ProductRepository
extends JpaRepository<Product, Integer> {
// nameʹҾ਺ͷจࣈྻ͕‫·ؚ‬Ε͍ͯΔ΋ͷΛ‫͢ࡧݕ‬Δ
List<Product> findByNameContaining(String keyword);
}

96
(C) CASAREAL, Inc. All rights reserved.

97.

ϝιου໊͔Βͷ+12-ࣗಈੜ੒ select product0_.id as id1_4_, product0_.category_id as category4_4_, product0_.name as name2_4_, product0_.price as price3_4_, product0_.vendor_id as vendor_i5_4_ from product product0_ where product0_.name like ? binding parameter [1] as [VARCHAR] - [%ύϚ%] 97 (C) CASAREAL, Inc. All rights reserved.

98.

ϝιου໊ʹ࢖͑ΔΩʔϫʔυʢҰ෦ʣ ▸ "OE 0S #FUXFFO -FTT5IBO  -FTT5IBO&RVBM -JLF /PU-JLF 4UBSUJOH8JUI  &OEJOH8JUI $POUBJOJOH 0SEFS#Z ʜ ▸ ৄࡉ͸ϦϑΝϨϯεࢀর ▸ IUUQTEPDTTQSJOHJPTQSJOHEBUBKQBEPDT DVSSFOUSFGFSFODFIUNMKQBRVFSZ NFUIPETRVFSZDSFBUJPO 98 (C) CASAREAL, Inc. All rights reserved.

99.

‫ڞ‬௨ඇνΣοΫྫ֎΁ͷม‫׵‬ ▸ +1"Ͱൃੜͨ͠ྫ֎͸%BUB"DDFTT&YDFQUJPO ͷαϒΫϥεʹม‫͞׵‬ΕΔ ▸ ͲΜͳσʔλΞΫηεٕज़Λ࢖͍ͬͯΔ͔Λ্Ґ૚ʹ ҙࣝͤ͞ͳ͍ %BUB"DDFTT&YDFQUJPO %BUB*OUFHSJUZ7JPMBUJPO &YDFQUJPO #BE4RM(SBNNBS &YDFQUJPO ɾɾɾ 99 (C) CASAREAL, Inc. All rights reserved.

100.

‫ࠪ؂‬ରԠ ▸ ࡞੒ऀɺ࡞੒೔࣌ɺߋ৽ऀɺߋ৽೔࣌Λࣗಈత ʹ%#ʹอଘͰ͖Δ @Entity public class Hoge { @CreatedBy String createdUser; @CreatedDate LocalDateTime createdDate; @LastModifiedBy String modifiedUser; @LastModifiedDate LocalDateTime lastModifiedDate; } 100 (C) CASAREAL, Inc. All rights reserved.

101.
[beta]
‫ࠪ؂‬ରԠʹඞཁͳઃఆ
Spring Security͔Β
@Component
Ϣʔβʔ໊Λऔಘͯ͠΋OK
public class MyAuditorAware
implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
return Optional.of("user01");}
}
@SpringBootApplication
@EnableJpaAuditing(auditorAwareRef = "myAuditorAware")
public class SpringDataJpaDaitokaiApplication { … }
101
(C) CASAREAL, Inc. All rights reserved.

102.
[beta]
‫ࠪ؂‬ରԠʹඞཁͳઃఆʢ͖ͭͮʣ
▸ TSDNBJOSFTPVSDFT.&5"*/'PSNYNMʹԼ‫ه‬Λ‫ه‬ड़
▸ ύεͱϑΝΠϧ໊Λ͜Εʹ͢ΔͱɺࣗಈͰಡΈࠐ·ΕΔ
<entity-mappings>
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener
class="org.springframework.data.jpa.domain.support.AuditingEntityListene
r" />
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
102
(C) CASAREAL, Inc. All rights reserved.

103.

%BUFBOE5JNF"1* +43 ରԠ ▸ +TS+QB$POWFSUFSTΫϥε ▸ -PDBM%BUFΫϥεͳͲͷ"UUSJCVUF$POWFSUFS͕͋Β͔ ͡Ίఏ‫͞ڙ‬Ε͍ͯΔ ▸ TFU1BDLBHF5P4DBO ͳͲͰ PSHTQSJOHGSBNFXPSLEBUBKQBDPOWFSUUISFFUFOύ οέʔδΛࢦఆ͢Ε͹0, ▸ 4QSJOH#PPUͰ͸"VUP$POpHVSBUJPOର৅ʹͳ͍ͬͯΔ 103 (C) CASAREAL, Inc. All rights reserved.

104.

ͦͷଞͷ‫ػ‬ೳʢࢼͯ͠ͳ͍ʣ ▸ !1SPDFEVSF QSPDFEVSF/BNFϓϩγʔδϟ ໊  ▸ &YBNQMF.BUDIFSʹΑΔλΠϓηʔϑ‫ه‬ड़ ▸ +QB$POUFYU 104 (C) CASAREAL, Inc. All rights reserved.

105.

·ͱΊ ▸ ศརͰ͢Ͷʂ ▸ 3FQPTJUPSZΠϯλϑΣʔε࡞Δ͚ͩ ▸ ϝιου໊͔Βͷ+12-ࣗಈੜ੒ ▸ ‫ڞ‬௨ඇνΣοΫྫ֎΁ͷม‫׵‬ ▸ ‫ࠪ؂‬ରԠ ▸ %BUFBOE5JNF"1* +43 ରԠ 105 (C) CASAREAL, Inc. All rights reserved.

106.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 106 (C) CASAREAL, Inc. All rights reserved.

107.

0QFO*O7JFX͕σϑΥϧτ ▸ &OUJUZ.BOBHFSͷϥΠϑαΠΫϧ͕Ϗϡʔ·Ͱͱ ͳ͍ͬͯΔʢʹϏϡʔͰͷ-B[ZϑΣονՄೳʣ 4QSJOH#PPUͷσϑΥϧτ ௨ৗͷ4QSJOH 7JFX $POUSPMMFS 4FSWJDF 3FQPTJUPSZ http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#boot-features-jpa-inweb-environment (C) CASAREAL, Inc. All rights reserved. 107

108.

0QFO*O7JFXΛΦϑʹ͢Δํ๏ ▸ BQQMJDBUJPOQSPQFSUJFTʹԼ‫ه‬ͷ‫ه‬ड़Λ௥Ճ͢Δ ͱɺ௨ৗͷ4QSJOHͱಉ͡ʢτϥϯβΫγϣϯ୯ ҐʣʹͳΔ spring.jpa.open-in-view=false 108 (C) CASAREAL, Inc. All rights reserved.

109.
[beta]
/BNJOH4USBUFHZ
▸ 4QSJOH1IZTJDBM/BNJOH4USBUFHZ͕σϑΥϧ
τͰద༻ࡁΈ
▸ Ωϟϝϧέʔε㲗εωʔΫέʔεͷ૬‫ޓ‬ม‫׵‬͸ࣗಈͰ
ߦΘΕΔ
@Entity public class Hoge {
// Spring BootͰ͸@Columnෆཁ
@Column(name=“foo_bar”) String fooBar;
}
http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#howto-configurehibernate-naming-strategy
(C) CASAREAL, Inc. All rights reserved.

109

110.

/BNJOH4USBUFHZͷมߋํ๏ ▸ BQQMJDBUJPOQSPQFSUJFTʹԼ‫ه‬ͷ‫ه‬ड़Λ௥Ճ spring.jpa.hibernate.naming.physical-strategy=StrategyΫϥεͷFQCN spring.jpa.hibernate.naming.implicit-strategy=StrategyΫϥεͷFQCN 110 (C) CASAREAL, Inc. All rights reserved.

111.
[beta]
!&OUJUZ4DBO
▸ ΤϯςΟςΟΫϥεͷύοέʔδ΍ɺ$POWFSUFS
ΫϥεͷύοέʔδΛࢦఆ
▸ +BWB$POpHΫϥεʹ෇Ճ͢Δ
@SpringBootApplication
@EntityScan(basePackages = {
"com.example.demo.persistence.entity",
"org.springframework.data.jpa.convert.threeten"})
public class SpringDataJpaDaitokaiApplication { … }
http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#howto-separate-entitydefinitions-from-spring-configuration
(C) CASAREAL, Inc. All rights reserved.

111

112.

ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼‫ݻ‬༗‫ػ‬ೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 112 (C) CASAREAL, Inc. All rights reserved.

113.

+1"͸ʮ࢖͑Δʯ͔ʁ ▸ ʮ࢖͑Δɻͨͩ͠ਖ਼͍͠஌͕ࣝඞཁɺ͔ͭγνϡ ΤʔγϣϯΛબͿʯͱ͍͏ͷ͕‫ݸ‬ਓతͳҙ‫Ͱݟ‬ ͢ 113 (C) CASAREAL, Inc. All rights reserved.

114.

+1"Λ࢖͍͍ͬͯ৚݅ ᶃ %#Λ৽‫ʹن‬ઃ‫͖Ͱܭ‬Δ ᶄ ू߹ԋࢉ΍'30.۟Ͱͷ෭໰߹ͤͳͲɺ ෳࡶͳ42-͸ཁ݅తʹগͳ͍ ᶅ ʮύʔϑΣΫτ+BWB&&ʯΛಡഁͨ͠ਓ͕ ϓϩδΣΫτʹਓҎ্͍Δ ͭͰ΋౰ͯ͸·Βͳ͍߲໨͕͋Ε͹ɺ ଞͷ03ϚούʔΛ࢖ͬͨํ͕͍͍͔΋😅 114 (C) CASAREAL, Inc. All rights reserved.

115.

ඞಡॻ੶ ▸ 4QSJOHపఈೖ໳ ▸ ֓ཁΛ஌ΔͳΒ͜ͷຊʂ ▸ ύʔϑΣΫτ+BWB&& ▸ +1"Λৄࡉ·Ͱ஌ΕΔʂ ▸ 4QSJOH%BUB+1"ϓϩάϥϛ ϯάೖ໳ ▸ 4QSJOH%BUB+1"ͷ֤‫ػ‬ೳΛֶ शʂ 115 (C) CASAREAL, Inc. All rights reserved.

116.

ඞಡࢿྉ ▸ ͸·Δʂ+1"ʢॳֶऀ޲͚ϥΠτ൛ʣ !TVLF@NBTB  ▸ IUUQTXXXTMJEFTIBSFOFUNBTBUPTIJUBEBKQB ▸ ͸·Δʂ+1" !NBLJOH͞Μ  ▸ IUUQTXXXTMJEFTIBSFOFUNBLJOHYKQB ▸ +1"ͷಉ࣮࣌ߦ੍‫ͱޚ‬ϩοΫ !TVLF@NBTB  ▸ IUUQTXXXTMJEFTIBSFOFUNBTBUPTIJUBEBKQBDDDSKKVH DDD ▸ !PQFOHM@͞Μͷ2JJUB‫ࣄه‬Ұ࿈ ▸ IUUQRJJUBDPNPQFOHMJUFNTGGBFDC 116 (C) CASAREAL, Inc. All rights reserved.

117.

ϦϑΝϨϯε ▸ 5FSBTPMVOB։ൃΨΠυϥΠϯ ▸ IUUQUFSBTPMVOBPSHHJUIVCJPHVJEFMJOF3&-&"4&KB "SDIJUFDUVSF*O%FUBJM%BUB"DDFTT%FUBJM%BUB"DDFTT+QBIUNM ▸ 4QSJOH%BUB+1" ▸ IUUQTEPDTTQSJOHJPTQSJOHEBUBKQBEPDTDVSSFOUSFGFSFODFIUNM ▸ )JCFSOBUF6TFS(VJEF ▸ IUUQEPDTKCPTTPSHIJCFSOBUFPSNVTFSHVJEFIUNM@TJOHMF )JCFSOBUF@6TFS@(VJEFIUNM ▸ +1" +43  ▸ IUUQTKDQPSHBCPVU+BWBDPNNVOJUZQSPDFTTpOBMKTSJOEFYIUNM 117 (C) CASAREAL, Inc. All rights reserved.

118.

&OKPZ4QSJOH%BUB+1" ▸ ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ 118 (C) CASAREAL, Inc. All rights reserved.