50分で分かるSpring Data JPA/spring-data-jpa-in-50min

50分で分かるSpring Data JPA/spring-data-jpa-in-50min

20170708 O/Rマッパー勉強会 in 大都会の資料です。

5dbaf4015e7f249ab21b195ced8e9e46?s=128

Masatoshi Tada

July 08, 2017
Tweet

Transcript

  1. 2.

    (C) CASAREAL, Inc. All rights reserved. ͜ͷηογϣϯʹ͍ͭͯ ▸ 4QSJOH%BUB+1"ͷجૅ஌ࣝΛஸೡʹղઆʂ ▸

    +1"੒෼͕ଟΊͰ͢ ▸ ຊ೔ͷαϯϓϧίʔυ͸(JU)VCʹ͋Γ·͢ ▸ IUUQTHJUIVCDPN.BTBUPTIJ5BEBTQSJOHEBUB KQBEBJUPLBJ 2
  2. 3.

    (C) CASAREAL, Inc. All rights reserved. ࣗݾ঺հ ▸ ଟాਅහʢ!TVLF@NBTBʣ ▸

    ݚमτϨʔφʔ!ΧαϨΞϧ ▸ େࡕग़਎ɾ߳઒ҭͪ ▸ 1JWPUBMೝఆߨࢣ ▸ ೔ຊ4QSJOHϢʔβʔձελοϑ ▸ ೔ຊ(MBTT'JTIϢʔβʔձӡӦϝϯόʔ 3
  3. 5.

    (C) CASAREAL, Inc. All rights reserved. $PSF4QSJOH<೔ؒ> ▸ %*͔Β"01ɾςετɾ8FCɾ%#ɾηΩϡϦ ςΟɾ.JDSPTFSWJDFT·Ͱɺͱͯ΋ೱް͔ͭ޿ൣ

    ғͳ೔ؒʂ ▸ %FQFOEFODZ*OKFDUJPO͚ͩͰ೔ؒ΋ʂ ▸ ࢖͍ํ͔Β಺෦ͷ࢓૊Έ·Ͱ
 పఈೖ໳ʂ 5
  4. 6.

    (C) CASAREAL, Inc. All rights reserved. 4QSJOH#PPU%FWFMPQFS<೔ؒ> ▸ جૅ͔ΒԠ༻·Ͱ޿ൣғʹֶशͰ͖Δʂ ▸

    4QSJOH#PPUͷ"VUP$POpHVSBUJPOͷ࢓૊ΈΛ ղઆʂ ▸ 8FCɺ%#ɺηΩϡϦςΟɺϝοηʔδϯά౳ 6
  5. 7.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 7
  6. 8.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 8
  7. 10.

    (C) CASAREAL, Inc. All rights reserved. 4QSJOH%BUBͱ͸ʁ ▸ ༷ʑͳσʔλΞΫηεٕज़Λಉ͡Α͏ ʹѻ͑ΔΑ͏ʹ͢ΔϓϩδΣΫτ

    ▸ "1*Λந৅Խɾڞ௨Խ ▸ ൃੜ͢Δྫ֎Λڞ௨ྫ֎ʹม׵ ▸ ϦϙδτϦΠϯλϑΣʔεͰ؆୯ʹॻ͚Δ ▸ +1"ɺ(FNpSFɺ.POHP%#ɺ3FEJTɺ $BTTBOESBͳͲଟ͘ͷٕज़ʹରԠ 10
  8. 11.

    (C) CASAREAL, Inc. All rights reserved. +1"ͱ͸ʁ ▸ +BWB&&ͷҰ෦ͱͯ͠ඪ४Խ ͞ΕͨσʔλΞΫηεٕज़ͷ

    ࢓༷ ▸ ࣮ମ͸+43ͱ͍͏จॻ ▸ ܾ·ͬͯΔͷ͸ΠϯλϑΣʔεɾ Ξϊςʔγϣϯɾྫ֎͕΄ͱΜ Ͳ 11
  9. 12.

    (C) CASAREAL, Inc. All rights reserved. +1"࣮૷ϥΠϒϥϦ ᶃ )JCFSOBUF ▸

    03Ϛούʔͷ૲෼͚తଘࡏ ▸ 4QSJOH%BUB+1"͸جຊతʹίϨ ᶄ &DMJQTF-JOL ▸ +1"ͷࢀর࣮૷ ▸ (MBTT'JTI8FC-PHJDͳͲʹ಺แ 12 ࠓճ͸ίϨ ※ଞͷ࣮૷͸JPA 2.1(Java EE 7)ʹ
 ɹରԠ͍ͯ͠ͳ͍
  10. 13.

    (C) CASAREAL, Inc. All rights reserved. +1"ͷಛ௃ ▸ %#੡඼ʹґଘ͠ͳ͍ૢ࡞͕هड़Ͱ͖Δ ▸

    ΤϯςΟςΟΛςʔϒϧͱରͰ࡞Δ ▸ ʮ4&-&$5ͷ݁ՌͱରʯͰ͸ͳ͍ͷͰ஫ҙʂ ▸ ରଟͷΑ͏ͳϦϨʔγϣϯΛදݱͰ͖Δ 13
  11. 14.

    (C) CASAREAL, Inc. All rights reserved. 4QSJOH%BUB+1"Λ࢖͓͏ ▸ 4QSJOH#PPUͷ৔߹ 14

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
  12. 15.

    (C) CASAREAL, Inc. All rights reserved. 4QSJOH%BUB+1"Λ࢖͓͏ ▸ ඇ4QSJOH#PPUͷ৔߹ 15

    <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.11.4.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.0.12.Final</version> </dependency>
  13. 16.

    (C) CASAREAL, Inc. All rights reserved. 42-ϩάͷग़ྗ͸ඞਢʂ ▸ τϥϒϧγϡʔςΟϯάͰॏཁʂ 16

    logging.level.org.hibernate.SQL=debug logging.level.org.hibernate.type.descriptor.sql.BasicBinder=\ trace <logger name="org.hibernate.SQL" level="DEBUG" /> <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" /> ▸ ඇ4QSJOH#PPUɿMPHCBDLYNMʹԼهΛઃఆ ▸ 4QSJOH#PPUɿBQQMJDBUJPOQSPQFSUJFTʹԼهΛઃఆ ※hibernate.show_sql=trueͩͱϩάͷϑΥʔϚοτΛࢦఆͰ͖ͳ͍ͷͰ͜ΕΒͷํ๏͕Φεεϝ
  14. 17.

    (C) CASAREAL, Inc. All rights reserved. 42-ϩάΛಡΈ΍͘͢͢Δ ▸ վߦ΍Πϯσϯτ͕ೖΔΑ͏ʹͳΔ 17

    spring.jpa.properties.hibernate.format_sql=true ▸ ඇ4QSJOH#PPUɿ+BWB$POpHʹԼهΛهड़ ▸ IUUQTHJUIVCDPN.BTBUPTIJ5BEBTQSJOHGPSTQSJOHCPPUCMPCNBTUFS TQSJOHTSDNBJOKBWBDPNFYBNQMFQFSTJTUFODFDPOpH +QB$POpHKBWB- ▸ 4QSJOH#PPUɿBQQMJDBUJPOQSPQFSUJFTʹԼهΛઃఆ
  15. 18.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 18
  16. 19.

    (C) CASAREAL, Inc. All rights reserved. +1"ͷΞʔΩςΫνϟ 19 &OUJUZ .BOBHFS

    ӬଓԽίϯςΩετ &OUJUZ &OUJUZ &OUJUZ ΫϥΠΞϯτ
 ϓϩάϥϜ DB &OUJUZ &OUJUZ .BOBHFS 'BDUPSZ ੜ੒
  17. 20.

    (C) CASAREAL, Inc. All rights reserved. ΤϯςΟςΟ ▸ جຊతʹςʔϒϧͱ ରͰ࡞Δ

    ▸ ʮ4&-&$5จͱର ʯͰ͸ͳ͍ʂ ▸ ओΩʔϑΟʔϧυඞ ਢ 20 @Entity public class Product { @Id private Integer id; private String name; // setter/getter }
  18. 21.

    (C) CASAREAL, Inc. All rights reserved. &OUJUZ.BOBHFS ▸ ΤϯςΟςΟͷʮঢ়ଶʯΛૢ࡞͢Δ૭ޱ ▸

    ʮ$36%ૢ࡞ͷ૭ޱʯͰ͸ͳ͍ͷͰ஫ҙʂ ▸ ݕࡧ݁Ռ͸ӬଓԽίϯςΩετʢޙड़ʣʹอଘ ▸ Πϯελϯε͸
 τϥϯβΫγϣϯ୯Ґ 21
  19. 22.

    (C) CASAREAL, Inc. All rights reserved. &OUJUZ.BOBHFS'BDUPSZ ▸ &OUJUZ.BOBHFSΛੜ੒͢Δ ▸

    Πϯεϯλϯε͸ΞϓϦέʔγϣϯͰͭͷΈ
 ʢʹγϯάϧτϯʣ 22
  20. 23.

    (C) CASAREAL, Inc. All rights reserved. ӬଓԽίϯςΩετ ▸ ΤϯςΟςΟͷΩϟογϡͷΑ͏ͳ΋ͷ ▸

    ΤϯςΟςΟ͕ӬଓԽίϯςΩετʹؚ·Ε͍ͯΔ͔ Ͳ͏͔͕ඇৗʹॏཁ ▸ τϥϯβΫγϣϯ୯ҐͰ
 ੜ੒ɾഁغ͞ΕΔ 23
  21. 24.

    (C) CASAREAL, Inc. All rights reserved. ΤϯςΟςΟͷʮঢ়ଶʯ 24 ঢ়ଶ આ໌

    NEW new͞Εͨ͹͔Γ MANAGED ӬଓԽίϯςΩετͰ؅ཧ͞Ε͍ͯΔ DETACHED ӬଓԽίϯςΩετ͔Β෼཭͞Ε͍ͯΔ REMOVED ࡟আ͕༧໿͞Ε͍ͯΔ
  22. 25.

    (C) CASAREAL, Inc. All rights reserved. &OUJUZ.BOBHFSͷϝιου 25 ϝιου આ໌

    find() ओΩʔݕࡧ persist() 1݅ͷӬଓԽ remove() 1݅ͷ࡟আ merge() ӬଓԽίϯςΩετ؅ཧԼʹ͢Δ detach() ӬଓԽίϯςΩετ؅ཧ֎ʹ͢Δ flush() มߋΛDB΁൓ө clear() ӬଓԽίϯςΩετΛΫϦΞ refresh() ΤϯςΟςΟͷ಺༰ΛDB࠷৽ʹ্ॻ͖
  23. 26.

    (C) CASAREAL, Inc. All rights reserved. ˒ॏཁ˒ΤϯςΟςΟͷঢ়ଶભҠ 26 ӬଓԽίϯςΩετ ."/"(&%

    ঢ়ଶ 3&.07&% ঢ়ଶ /&8 ঢ়ଶ %&5"$)&% ঢ়ଶ persist() detach()
 clear() merge() remove() flush() flush() refresh() find()
 JPQL detach()
 clear()
  24. 27.

    (C) CASAREAL, Inc. All rights reserved. ओΩʔݕࡧ 27 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=? ͜ΜͳSQL͕
 ൃߦ͞ΕΔ
  25. 28.

    (C) CASAREAL, Inc. All rights reserved. ௥Ճ 28 Product product

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

    (C) CASAREAL, Inc. All rights reserved. ߋ৽ 29 // find()Ͱݕࡧ͢ΔͱMANAGEDঢ়ଶ

    Product product = em.find(Product.class, 1); // ஋ͷߋ৽ product.setName("ߋ৽ޙͷ໊લ"); // ߋ৽ͷ൓ө(ίϛοτͰ΋OK) em.flush(); ▸ ."/"(&%ঢ়ଶͷΤϯςΟςΟʹTFUUFSͰ஋Λ ߋ৽ˠ൓ө
  27. 30.

    (C) CASAREAL, Inc. All rights reserved. ߋ৽ 30 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]
  28. 31.

    (C) CASAREAL, Inc. All rights reserved. NFSHF ʹΑΔߋ৽ 31 Product

    product = new Product(); product.setId(1); product.setName(“ߋ৽ޙͷ໊લ"); // merge()Ͱ΋ߋ৽Ͱ͖Δ em.merge(product); em.flush(); ▸ OFXͨ͠ΤϯςΟςΟΛNFSHF ͢Δ
  29. 32.

    (C) CASAREAL, Inc. All rights reserved. NFSHF ʹΑΔߋ৽ 32 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] ▸ 4&-&$5จʴ61%"5&จ͕ൃߦ͞ΕΔ
  30. 33.

    (C) CASAREAL, Inc. All rights reserved. NFSHF ͷམͱ݀͠ ▸ Ҿ਺ΤϯςΟςΟͷίϐʔ͕࡞ΒΕɺͦΕ͕໭

    Γ஋͔ͭ."/"(&%ঢ়ଶʹͳΔ ▸ Ҿ਺ࣗମ͸."/"(&%ঢ়ଶʹͳΒͳ͍ͷͰ஫ҙ 33 Product mergedProduct = em.merge(product); // ໭Γ஋͸MANAGEDঢ়ଶ assertTrue(em.contains(mergedProduct)); // Ҿ਺ͷঢ়ଶ͸มΘΒͳ͍ assertFalse(em.contains(product));
  31. 34.

    (C) CASAREAL, Inc. All rights reserved. ࡟আ 34 // find()Ͱݕࡧ͢ΔͱMANAGEDঢ়ଶ

    Product product = em.find(Product.class, 30); // REMOVEDঢ়ଶʹ͢Δ em.remove(product); // ߋ৽ͷ൓ө(ίϛοτͰ΋OK) em.flush(); ▸ ."/"(&%ঢ়ଶͷΤϯςΟςΟΛSFNPWF ͢Δ
  32. 35.

    (C) CASAREAL, Inc. All rights reserved. ࡟আ 35 select …

    from product product0_ where product0_.id=? binding parameter [1] as [INTEGER] - [30] delete from product where id=? binding parameter [1] as [INTEGER] - [30]
  33. 36.

    (C) CASAREAL, Inc. All rights reserved. ࡟আͷΩϟϯηϧ 36 // find()Ͱݕࡧ͢ΔͱMANAGEDঢ়ଶ

    Product product = em.find(Product.class, 30); // REMOVEDঢ়ଶʹ͢Δ em.remove(product); // DETACHEDঢ়ଶʹ͢Δ em.detach(product); // DETACHEDঢ়ଶͳͷͰDBʹ൓ө͞Εͳ͍ em.flush(); ▸ EFUBDI ·ͨ͸DMFBS Ͱ%&5"$)&%ঢ়ଶʹ͢Δ
  34. 37.

    (C) CASAREAL, Inc. All rights reserved. ·ͱΊ ▸ ΤϯςΟςΟͷʮঢ়ଶʯʹ஫ҙ ▸

    pOE ͰओΩʔݕࡧ ▸ QFSTJTU Ͱ௥Ճ ▸ ߋ৽͸pOE  TFUUFSͰߋ৽ ▸ SFNPWF Ͱ࡟আ 37
  35. 38.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 38
  36. 39.

    (C) CASAREAL, Inc. All rights reserved. +12-ͱ͸ʁ ▸ 42-ϥΠΫͳจ๏Ͱ4&-&$561%"5&%&-&5& Λهड़͢Δ

    ▸ */4&35͸ແ͍ ▸ %#੡඼ʹґଘ͠ͳ͍ΫΤϦ͕هड़Մೳ ▸ %#ݻ༗ػೳʹ͍ͭͯ͸ޙड़ 39
  37. 40.

    (C) CASAREAL, Inc. All rights reserved. جຊతͳ࢖͍ํ 40 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(); ▸ ୯Ұݕࡧ ▸ ෳ਺ݕࡧ
  38. 41.

    (C) CASAREAL, Inc. All rights reserved. ࣮ߦ͢Δͱ42-ʹ຋༁͞ΕΔ 41 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=? ▸ ͲΜͳ42-͕ൃߦ͞ΕΔ͔͸+1"࢓༷Ͱະఆٛ
 ʹ׬શʹ࣮૷ґଘʢԼه͸)JCFSOBUFͷྫʣ
  39. 42.

    (C) CASAREAL, Inc. All rights reserved. +12-ͷجຊจ๏ 42 SELECT ࣝผม਺

    FROM ΤϯςΟςΟ໊ [AS] ࣝผม਺ WHERE ࣝผม਺.ϓϩύςΟ໊ <= :ύϥϝʔλ໊ ▸ ࣝผม਺ʹΤϯςΟςΟͷผ໊ʢඞਢʣ ▸ ΤϯςΟςΟ໊΍ϓϩύςΟ໊͸ɺ
 େจࣈখจࣈΛݫີʹ۠ผ͢ΔͷͰ஫ҙʂ ▸ 4&-&$5౳͸۠ผ͠ͳ͍͕େจࣈͷํ͕ؒҧ͑ʹ͍͘
  40. 43.

    (C) CASAREAL, Inc. All rights reserved. ίϯετϥΫλࣜ ▸ ඇΤϯςΟςΟͳΫϥεʹ΋ϚοϐϯάՄೳ ▸

    ओͳ༻్͸ूܭ 43 SELECT new com.example.demo.dto.CountDto( od.product.id, COUNT(od)) FROM OrderDetail od GROUP BY od.product.id ύοέʔδ໊෇͖ͳͷ͕ਏ͍ʜ
  41. 44.

    (C) CASAREAL, Inc. All rights reserved. ͦͷଞͷ+12-Ͱग़དྷΔ͜ͱᶃ ▸ ݁߹ʢ*//&3+0*/ɺ-&'5065&3+0*/ɻৄࡉ͸ޙ ड़ʣ

    ▸ #&58&&/ɺ*/ɺ-*,&ɺ&9*454ɺ"--ɺ"/:ɺ$"4& ▸ (3061#:ɺ)"7*/( ▸ ूܭؔ਺ʢ."9 ɺ.*/ ɺ$06/5 ɺ46. ɺ "7( ͷͭʣ ▸ ෭໰߹ͤʢ8)&3&۟ͱ)"7*/(۟ͰͷΈʣ 44
  42. 45.

    (C) CASAREAL, Inc. All rights reserved. ͦͷଞͷ+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ʣ ▸ %#ݻ༗ؔ਺ͷݺͼग़͠ʢޙड़ʣ 45
  43. 46.

    (C) CASAREAL, Inc. All rights reserved. +12-Ͱग़དྷͳ͍͜ͱ ▸ */4&35 ▸

    ू߹ԋࢉʢ6/*0/ɺ6/*0/"--ɺ */5&34&$5ɺ&9$&15ʣ ▸ '30.۟Ͱͷ෭໰߹ͤ 46
  44. 48.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 48
  45. 49.

    (C) CASAREAL, Inc. All rights reserved. ϦϨʔγϣϯΛදݱ͢Δ ▸ !0OF5P0OF ▸

    !0OF5P.BOZ ▸ !.BOZ5P0OF ▸ !.BOZ5P.BOZ 49 @Entity public class Product { … @ManyToOne @JoinColumn(name="vendor_id") private Vendor vendor; } @Entity public class Vendor { … }
  46. 50.

    (C) CASAREAL, Inc. All rights reserved. ϑΣονͱ͸ ▸ ͋ΔΤϯςΟςΟΛಡΈࠐΜͩ࣌ɺϦϨʔγϣϯઌ ͷΤϯςΟςΟΛ͍ͭಡΈࠐΉ͔

    ᶃ &"(&3ϑΣονɿଈ࣌ಡΈࠐΈ ▸ ಉ࣌ʹಡΈࠐΉʢ)JCFSOBUFͩͱ+0*/ɺ&DMJQTF-JOLͩͱ 4&-&$5จ͕ෳ਺ಉ࣌ʹൃߦʣ ᶄ -";:ϑΣονɿ஗ԆಡΈࠐΈ ▸ ඞཁʹͳͬͨ࣌ʢHFUUFS͕ݺ͹Εͨ࣌ʣʹಡΈࠐΉ 50
  47. 51.

    (C) CASAREAL, Inc. All rights reserved. ϑΣονͷࢦఆํ๏ ▸ !9YY5P9YYͷGFUDIଐੑʹࢦఆ͢Δ ▸

    !9YY5P.BOZˠσϑΥϧτͰ-";: ▸ !9YY5P0OFˠσϑΥϧτͰ&"(&3 51 @Entity public class Product { … @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="vendor_id") private Vendor vendor; }
  48. 52.

    (C) CASAREAL, Inc. All rights reserved. -";:ϑΣονͷྫ 52 select (ྻলུ)

    from product product0_ where product0_.id=? select (ྻলུ) from vendor vendor0_ where vendor0_.id=? @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
  49. 53.

    (C) CASAREAL, Inc. All rights reserved. -";:ϑΣονͷ஫ҙ఺ ▸ ӬଓԽίϯςΩετഁغޙʢ㲈τϥϯβΫγϣϯ ऴྃޙʣʹϑΣον͢Δͱྫ֎͕ൃੜ͢Δ

    53 em.getTransaction().begin(); Product p = em.find(Product.class, 1); em.getTransaction().rollback(); Vendor v = p.getVendor(); String n = v.getName(); // ͜͜Ͱྫ֎ൃੜ
  50. 54.

    (C) CASAREAL, Inc. All rights reserved. &"(&3ϑΣονͷྫ 54 @Entity public

    class Product { @ManyToOne(fetch = FetchType.EAGER) private Vendor vendor; } 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=? 1ճͷΈ SELECT
  51. 55.

    (C) CASAREAL, Inc. All rights reserved. ϑΣον͸جຊ-";:ʂ ▸ &"(&3ʹ͢Δͱෆཁͳσʔλ·ͰಡΈࠐΉ͔Β ▸

    !0OF5P.BOZͰେྔͷσʔλΛಡΈࠐΉͱ͔ڪා
 ʢʹ/ ໰୊ʣ ▸ -";:͔Β&"(&3ʹಈతʹมߋ͍ͨ͠৔߹͸ɺ +12-ͷ+0*/'&5$)จΛར༻͢Δ 55
  52. 56.

    (C) CASAREAL, Inc. All rights reserved. / ໰୊͕ى͜Δྫ 56 //

    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); } }
  53. 57.

    (C) CASAREAL, Inc. All rights reserved. ୯ͳΔ+0*/͸ҙຯ͕ͳ͍ 57 // 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); } }
  54. 58.

    (C) CASAREAL, Inc. All rights reserved. ୯ͳΔ+0*/͸ҙຯ͕ͳ͍ 58 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ͷྻ
 ͔͠औಘͯ͠ͳ͍
  55. 59.

    (C) CASAREAL, Inc. All rights reserved. +0*/'&5$)จͰղܾʂʁ ▸ -";:ʹͳ͍ͬͯΔϦϨʔγϣϯΛಈతʹ&"(&3 ʹมߋͯ͘͠ΕΔ

    59 // order_summary΁ͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ List<OrderSummary> list = em.createQuery( "SELECT os FROM OrderSummary os" + " JOIN FETCH os.orderDetailList" + , OrderSummary.class) .getResultList();
  56. 60.

    (C) CASAREAL, Inc. All rights reserved. +0*/'&5$)จͰղܾʂʁ 60 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ͯ͠
 શྻऔಘ
  57. 61.

    (C) CASAREAL, Inc. All rights reserved. දࣔ͢Δͱɾɾɾ 61 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} ɾɾɾ ಉ͡σʔλ͕
 Կճ΋ʂʁ
  58. 62.

    (C) CASAREAL, Inc. All rights reserved. +0*/'&5$) %*45*/$5Ͱղܾʂ 62 //

    order_summary΁ͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ List<OrderSummary> list = em.createQuery( "SELECT DISTINCT os FROM OrderSummary os" + " JOIN FETCH os.orderDetailList" + , OrderSummary.class) .getResultList();
  59. 63.

    (C) CASAREAL, Inc. All rights reserved. +0*/'&5$) %*45*/$5Ͱղܾʂ 63 select

    distinct 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=? distinct͕෇͕͘
 SQLͷ݁Ռ͸
 ͜ͷ৔߹͸ಉ͡
  60. 64.

    (C) CASAREAL, Inc. All rights reserved. දࣔ͢Δͱɾɾɾ 64 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} σʔλͷॏෳ͕
 ແ͘ͳͬͨʂ
  61. 66.

    (C) CASAREAL, Inc. All rights reserved. // 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(); ωετͨ͠+0*/'&5$) ▸ )JCFSOBUFͰͷΈΤΠϦΞεΛ෇͚Δ͜ͱ͕ग़དྷ Δ 66 ΤΠϦΞε
  62. 67.

    (C) CASAREAL, Inc. All rights reserved. ωετͨ͠+0*/'&5$) 67 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ͯ͠
 શྻऔಘ
  63. 68.

    (C) CASAREAL, Inc. All rights reserved. ·ͱΊ ▸ 'FUDI5ZQF͸-";:Λࢦఆ͠Α͏ʂ ▸

    +0*/'&5$)จ %*45*/$5۟Ͱ/ ໰୊Λղ ܾͰ͖Δʂ ▸ ωετͨ͠+0*/'&5$)͸)JCFSOBUFͷΈʂ 68
  64. 69.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 69
  65. 70.

    (C) CASAREAL, Inc. All rights reserved. +1"Ͱར༻Ͱ͖Δ%#ݻ༗ػೳ ▸ ωΠςΟϒ42- ▸

    +12-͔Βͷ%#ݻ༗ؔ਺ݺͼग़͠ ▸ ετΞυϓϩγʔδϟ ▸ ΠϯσοΫε 70
  66. 71.

    (C) CASAREAL, Inc. All rights reserved. ωΠςΟϒ42- ▸ Ϛοϐϯάઌ͸ඞͣΤϯςΟςΟΫϥε ▸

    جຊతʹશྻͷ4&-&$5͕ඞཁ ▸ δΣωϦΫε͕࢖͑ͳ͍ 71 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();
  67. 72.

    (C) CASAREAL, Inc. All rights reserved. ΤϯςΟςΟʹϚοϐϯά͠ͳ͍ํ๏ ▸ ໭Γ஋Λ0CKFDU<>ʹ͢Δ 72

    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]; Ωϟετ͕ඞཁ
  68. 74.

    (C) CASAREAL, Inc. All rights reserved. !4RM3FTVMU4FU.BQQJOH 74 @Entity @SqlResultSetMapping(

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

    (C) CASAREAL, Inc. All rights reserved. !4RM3FTVMU4FU.BQQJOH 75 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(); public class ProductDto { private Integer id; private String name; public ProductDto(Integer id, String name) { … } } ΤϯςΟςΟͰͳ͍
 ೚ҙͷΫϥε
  70. 76.

    (C) CASAREAL, Inc. All rights reserved. +12-͔Βͷ%#ݻ༗ؔ਺ݺͼग़͠ ▸ '6/$5*0/ ؔ਺໊

    Ҿ਺ Ҿ਺ ʜ 76 // 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();
  71. 77.

    (C) CASAREAL, Inc. All rights reserved. ετΞυϓϩγʔδϟ ▸ DSFBUF4UPSFE1SPDFEVSF2VFSZ 77

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

    (C) CASAREAL, Inc. All rights reserved. ΠϯσοΫε ▸ !*OEFY 78

    @Entity @Index(members = {"name"}) public class Product { … } ※ΠϯσοΫεʹৄ͘͠ͳ͍ͨΊචऀ͸ະݕূͰ͢ɻͥͻࢼͯ͠ϑΟʔυόοΫ͍ͩ͘͞ʂ
 ※@IndicesΛར༻ͯ͠ෳ਺ͷ@IndexΛࢦఆՄೳ
  73. 79.

    (C) CASAREAL, Inc. All rights reserved. %#ݻ༗ػೳͷར༻͸࠷ऴखஈ ▸ ʮ%#੡඼͕มΘͬͯ΋ίʔυͷมߋ͕ඞཁͳ͍ʯ ͱ͍͏+1"ͷϝϦοτ͕ࣦΘΕΔ

    ▸ ར༻ͤ͟ΔΛಘͳ͍Օॴ͸ɺυΩϡϝϯτͱ͔ ʹ࢒͓͍ͯͨ͠΄͏͕Α͍ ▸ ͦ͏͠ͳ͍ͱ਺೥ޙʹ%#ΛҠ২͢Δ͜ͱʹͳͬͨͱ ͖େม 79
  74. 81.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 81
  75. 82.

    (C) CASAREAL, Inc. All rights reserved. 4QSJOH%BUB+1"ͷԼ४උ ▸ -PDBM$POUBJOFS&OUJUZ.BOBHFS'BDUPSZ#FBO Λ#FBOఆٛ

    ▸ &OUJUZ.BOBHFS'BDUPSZ͕ࣗಈͰ#FBOͱͳΔ ▸ 4QSJOH#PPUͰ͸ఆٛࡁΈ ▸ ৄࡉ͸੿ஶʮ4QSJOHGPS4QSJOH#PPUʯ΁ ▸ IUUQTTQFBLFSEFDLDPNNBTBUPTIJUBEBTQSJOHGPS TQSJOHCPPUOVNCFSKTVH 82
  76. 83.

    (C) CASAREAL, Inc. All rights reserved. 4QSJOH%BUB+1"ͷศརػೳ ▸ 3FQPTJUPSZΠϯλϑΣʔε࡞Δ͚ͩ ▸

    ϝιου໊͔Βͷ+12-ࣗಈੜ੒ ▸ ڞ௨ඇνΣοΫྫ֎΁ͷม׵ ▸ ؂ࠪରԠ ▸ %BUFBOE5JNF"1* +43 ରԠ 83
  77. 84.

    (C) CASAREAL, Inc. All rights reserved. 3FQPTJUPSZΠϯλϑΣʔε ▸ +QB3FQPTJUPSZΛܧঝͨ͠ΠϯλϑΣʔεΛ࡞ Δ͚ͩͰɺجຊతͳ$36%ॲཧ͸࡞੒׬ྃ

    84 public interface ProductRepository extends JpaRepository<Product, Integer> { // த͸ۭͰOK } ※@RepositoryΞϊςʔγϣϯ͸ඞཁ͋Γ·ͤΜ
  78. 85.

    (C) CASAREAL, Inc. All rights reserved. +QB3FQPTJUPSZͷओͳϝιου ▸ pOE0OF JE

    pOE"MM pOE"MM TPSU  pOE"MM JET pOE"MM QBHFBCMF  ▸ TBWF FOUJUZ ˞௥Ճ΋ߋ৽΋Մೳ ▸ EFMFUF JE EFMFUF FOUJUZ  ▸ DPVOU FYJTUT JE 85
  79. 86.

    (C) CASAREAL, Inc. All rights reserved. 3FQPTJUPSZΠϯλϑΣʔεͷར༻ 86 @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͚ͭͩͷ৔߹ͷΈʣ
  80. 87.

    (C) CASAREAL, Inc. All rights reserved. !2VFSZ ▸ 3FQPTJUPSZΠϯλϑΣʔεʹɺ೚ҙͷ+12-Λ࣮ ߦ͢ΔϝιουΛ௥ՃͰ͖Δ

    ▸ OBUJWF2VFSZUSVFͱ͢Ε͹ωΠςΟϒ42-΋ॻ͚Δ 87 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); }
  81. 88.

    (C) CASAREAL, Inc. All rights reserved. !.PEJGZJOH ▸ !2VFSZͰ61%"5&จ΍%&-&5&จΛࢦఆͨ͠ ৔߹͸ඞཁ

    88 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); }
  82. 89.

    (C) CASAREAL, Inc. All rights reserved. !-PDL ▸ -PDL.PEF5ZQFΛࢦఆָͯ͠؍తϩοΫɾ൵؍ తϩοΫΛࢦఆՄೳ

    89 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
  83. 90.

    (C) CASAREAL, Inc. All rights reserved. !&OUJUZ(SBQI ▸ &OUJUZ(SBQIΛར༻ͨ͠ಈతͳϑΣονͷมߋ ͕Մೳ

    90 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
  84. 91.

    (C) CASAREAL, Inc. All rights reserved. ϝιου໊͔Βͷ+12-ࣗಈੜ੒ ▸ ϧʔϧʹैͬͯϝιου໊Λهड़͢Ε͹ɺ !2VFSZͰ+12-Λهड़͢Δඞཁͳ͠

    91 public interface ProductRepository extends JpaRepository<Product, Integer> { // nameʹҾ਺ͷจࣈྻؚ͕·Ε͍ͯΔ΋ͷΛݕࡧ͢Δ List<Product> findByNameContaining(String keyword); }
  85. 92.

    (C) CASAREAL, Inc. All rights reserved. ϝιου໊͔Βͷ+12-ࣗಈੜ੒ 92 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] - [%ύϚ%]
  86. 93.

    (C) CASAREAL, Inc. All rights reserved. ϝιου໊ʹ࢖͑ΔΩʔϫʔυʢҰ෦ʣ ▸ "OE 0S

    #FUXFFO -FTT5IBO  -FTT5IBO&RVBM -JLF /PU-JLF 4UBSUJOH8JUI  &OEJOH8JUI $POUBJOJOH 0SEFS#Z ʜ ▸ ৄࡉ͸ϦϑΝϨϯεࢀর ▸ IUUQTEPDTTQSJOHJPTQSJOHEBUBKQBEPDT DVSSFOUSFGFSFODFIUNMKQBRVFSZ NFUIPETRVFSZDSFBUJPO 93
  87. 94.

    (C) CASAREAL, Inc. All rights reserved. ڞ௨ඇνΣοΫྫ֎΁ͷม׵ ▸ +1"Ͱൃੜͨ͠ྫ֎͸%BUB"DDFTT&YDFQUJPO ͷαϒΫϥεʹม׵͞ΕΔ

    ▸ ͲΜͳσʔλΞΫηεٕज़Λ࢖͍ͬͯΔ͔Λ্Ґ૚ʹ ҙࣝͤ͞ͳ͍ 94 %BUB"DDFTT&YDFQUJPO %BUB*OUFHSJUZ7JPMBUJPO
 &YDFQUJPO #BE4RM(SBNNBS &YDFQUJPO ɾɾɾ
  88. 95.

    (C) CASAREAL, Inc. All rights reserved. ؂ࠪରԠ ▸ ࡞੒ऀɺ࡞੒೔࣌ɺߋ৽ऀɺߋ৽೔࣌Λࣗಈత ʹ%#ʹอଘͰ͖Δ

    95 @Entity public class Hoge { @CreatedBy String createdUser; @CreatedDate LocalDateTime createdDate; @LastModifiedBy String modifiedUser; @LastModifiedDate LocalDateTime lastModifiedDate; }
  89. 96.

    (C) CASAREAL, Inc. All rights reserved. ؂ࠪରԠʹඞཁͳઃఆ 96 @SpringBootApplication @EnableJpaAuditing(auditorAwareRef

    = "myAuditorAware") public class SpringDataJpaDaitokaiApplication { … } @Component public class MyAuditorAware implements AuditorAware<String> { @Override public String getCurrentAuditor() {return "user01";} } Spring Security͔Β
 Ϣʔβʔ໊Λऔಘͯ͠΋OK
  90. 97.

    (C) CASAREAL, Inc. All rights reserved. ؂ࠪରԠʹඞཁͳઃఆʢ͖ͭͮʣ 97 <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> ▸ TSDNBJOSFTPVSDFT.&5"*/'PSNYNMʹԼهΛهड़ ▸ ύεͱϑΝΠϧ໊Λ͜Εʹ͢ΔͱɺࣗಈͰಡΈࠐ·ΕΔ
  91. 98.

    (C) CASAREAL, Inc. All rights reserved. %BUFBOE5JNF"1* +43 ରԠ ▸

    +TS+QB$POWFSUFSTΫϥε ▸ -PDBM%BUFΫϥεͳͲͷ"UUSJCVUF$POWFSUFS͕͋Β͔ ͡Ίఏڙ͞Ε͍ͯΔ ▸ TFU1BDLBHF5P4DBO ͳͲͰ PSHTQSJOHGSBNFXPSLEBUBKQBDPOWFSUUISFFUFOύ οέʔδΛࢦఆ͢Ε͹0, 98
  92. 100.

    (C) CASAREAL, Inc. All rights reserved. ·ͱΊ ▸ ศརͰ͢Ͷʂ ▸

    3FQPTJUPSZΠϯλϑΣʔε࡞Δ͚ͩ ▸ ϝιου໊͔Βͷ+12-ࣗಈੜ੒ ▸ ڞ௨ඇνΣοΫྫ֎΁ͷม׵ ▸ ؂ࠪରԠ ▸ %BUFBOE5JNF"1* +43 ରԠ 100
  93. 101.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 101
  94. 102.

    (C) CASAREAL, Inc. All rights reserved. 0QFO*O7JFX͕σϑΥϧτ ▸ &OUJUZ.BOBHFSͷϥΠϑαΠΫϧ͕Ϗϡʔ·Ͱͱ ͳ͍ͬͯΔʢʹϏϡʔͰͷ-B[ZϑΣονՄೳʣ

    102 3FQPTJUPSZ 4FSWJDF $POUSPMMFS 7JFX ௨ৗͷ4QSJOH 4QSJOH#PPUͷσϑΥϧτ http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#boot-features-jpa-in- web-environment
  95. 104.

    (C) CASAREAL, Inc. All rights reserved. /BNJOH4USBUFHZ ▸ 4QSJOH1IZTJDBM/BNJOH4USBUFHZ͕σϑΥϧ τͰద༻ࡁΈ

    ▸ Ωϟϝϧέʔε㲗εωʔΫέʔεͷ૬ޓม׵͸ࣗಈͰ ߦΘΕΔ 104 @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-configure- hibernate-naming-strategy
  96. 105.

    (C) CASAREAL, Inc. All rights reserved. /BNJOH4USBUFHZͷมߋํ๏ ▸ BQQMJDBUJPOQSPQFSUJFTʹԼهͷهड़Λ௥Ճ 105

    spring.jpa.hibernate.naming.physical-strategy=StrategyΫϥεͷFQCN spring.jpa.hibernate.naming.implicit-strategy=StrategyΫϥεͷFQCN
  97. 106.

    (C) CASAREAL, Inc. All rights reserved. !&OUJUZ4DBO ▸ ΤϯςΟςΟΫϥεͷύοέʔδ΍ɺ$POWFSUFS ΫϥεͷύοέʔδΛࢦఆ

    ▸ +BWB$POpHΫϥεʹ෇Ճ͢Δ 106 @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-entity- definitions-from-spring-configuration
  98. 107.

    (C) CASAREAL, Inc. All rights reserved. ᶃ +1"4QSJOH%BUB+1"ͱ͸ʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶ؅ཧ

    ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12-  ᶆ ϦϨʔγϣϯͱ/ ໰୊ରࡦ ᶇ %#੡඼ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷ஫ҙ఺ ᶊ +1"͸ʮ࢖͑Δʯͷ͔ʁ 107
  99. 109.

    (C) CASAREAL, Inc. All rights reserved. +1"Λ࢖͍͍ͬͯ৚݅ ᶃ %#Λ৽نʹઃܭͰ͖Δ ᶄ

    ू߹ԋࢉ΍'30.۟Ͱͷ෭໰߹ͤͳͲɺ
 ෳࡶͳ42-͸ཁ݅తʹগͳ͍ ᶅ ʮύʔϑΣΫτ+BWB&&ʯΛಡഁͨ͠ਓ͕
 ϓϩδΣΫτʹਓҎ্͍Δ 109 ͭͰ΋౰ͯ͸·Βͳ͍߲໨͕͋Ε͹ɺ ଞͷ03ϚούʔΛ࢖ͬͨํ͕͍͍͔΋
  100. 110.

    (C) CASAREAL, Inc. All rights reserved. ඞಡॻ੶ ▸ 4QSJOHపఈೖ໳ ▸

    ֓ཁΛ஌ΔͳΒ͜ͷຊʂ 110 ▸ ύʔϑΣΫτ+BWB&& ▸ +1"Λৄࡉ·Ͱ஌ΕΔʂ
  101. 111.

    (C) CASAREAL, Inc. All rights reserved. ඞಡࢿྉ ▸ ͸·Δʂ+1"ʢॳֶऀ޲͚ϥΠτ൛ʣ !TVLF@NBTB

     ▸ IUUQTXXXTMJEFTIBSFOFUNBTBUPTIJUBEBKQB ▸ ͸·Δʂ+1" !NBLJOH͞Μ  ▸ IUUQTXXXTMJEFTIBSFOFUNBLJOHYKQB ▸ +1"ͷಉ࣮࣌ߦ੍ޚͱϩοΫ !TVLF@NBTB  ▸ IUUQTXXXTMJEFTIBSFOFUNBTBUPTIJUBEBKQBDDDSKKVH DDD ▸ !PQFOHM@͞Μͷ2JJUBهࣄҰ࿈ ▸ IUUQRJJUBDPNPQFOHMJUFNTGGBFDC 111
  102. 112.

    (C) CASAREAL, Inc. All rights reserved. ϦϑΝϨϯε ▸ 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 112