10K Views
May 26, 22
スライド概要
JSUG Spring Fest 2017での発表資料です。
Java、Spring、IntelliJ IDEA
1 1JWPUBMೝఆߨࢣ͕͑ڭΔʂ 4QSJOH%BUB+1"ʹΑΔ σʔλΞΫηεపఈೖ גΧαϨΞϧଟాਅහ +46(4QSJOH'FTU ݄ ۚ (C) CASAREAL, Inc. All rights reserved.
<࣭>03ϚούʔɺԿΛͬͯ·͔͢ʁ ᶃ +1"PS)JCFSOBUF ᶄ .Z#BUJT ᶅ 4QSJOH+%#$ ᶆ ͦͷଞ 2 (C) CASAREAL, Inc. All rights reserved.
<࣭>ͳͥ+1"Λબͼ·͔ͨ͠ʁ ᶃ ඪ४ٕज़͔ͩΒ ᶄ ੈͷதʹใ͕ଟ͍͔Β ᶅ ཁ݅ʹద͍͔ͯͨ͠Β ᶆ ͦͷଞ 3 (C) CASAREAL, Inc. All rights reserved.
͜ͷηογϣϯʹ͍ͭͯ ▸ 4QSJOH%BUB+1"ͷૅجࣝΛஸೡʹղઆʂ ▸ +1"͕ଟΊͰ͢😅 ▸ ຊͷαϯϓϧίʔυ(JU)VCʹ͋Γ·͢ ▸ IUUQTHJUIVCDPN.BTBUPTIJ5BEBTQSJOHEBUB KQBEBJUPLBJ 4 (C) CASAREAL, Inc. All rights reserved.
ࣗݾհ ▸ ଟాਅහʢ!TVLF@NBTBʣ ▸ ݚमτϨʔφʔ!ΧαϨΞϧ ▸ 4QSJOH+BWB&& ▸ 1JWPUBMೝఆߨࢣʢࠃͰਓʣ ▸ +46(ελοϑɺ(6(+ӡӦϝϯόʔ 5 (C) CASAREAL, Inc. All rights reserved.
ΧαϨΞϧձࣾհ ▸ ৭ΜͳϓϩάϥϛϯάޠݴͷݚमΛͬͯ·͢ʂ 6 (C) CASAREAL, Inc. All rights reserved.
$PSF4QSJOH ▸ 1JWPUBMೝఆτϨʔχϯά ▸ ओཁςΫϊϩδʔΛཏ ▸ ෦ͷΈ·Ͱৄࡉʹֶशʢ%*͚ͩͰؒʣ ▸ ࣍ճ࣮ࢪ͔Β4QSJOHʹରԠʂ ▸ ݄ʙɺʙͷܭؒ ▸ IUUQTXXXDBTBSFBMDPKQMTTFSWJDFPQFOTFNJOBSQJWPUBM Q 7 (C) CASAREAL, Inc. All rights reserved.
ͦͷଞͷ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.
<ࠂ>1JWPUBM$MPVE'PVOESZϋϯζΦϯ ▸ ݄ ▸ 1JWPUBM$MPVE'PVOESZΛར༻ͨ͠ΞϓϦέʔ γϣϯ։ൃΛମ͢·͖Ͱݧʂ ▸ ແྉ ▸ IUUQTXXXDBTBSFBMDPKQMTTFSWJDF FWFOUTFNJOBSMJOFVQGT 9 (C) CASAREAL, Inc. All rights reserved.
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 10 (C) CASAREAL, Inc. All rights reserved.
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 11 (C) CASAREAL, Inc. All rights reserved.
4QSJOH%BUB+1"ͱʁ ▸ 4QSJOH%BUB+1"4QSJOH%BUB +1" 12 (C) CASAREAL, Inc. All rights reserved.
4QSJOH%BUBͱʁ ▸ ༷ʑͳσʔλΞΫηεٕज़Λಉ͡Α͏ ʹѻ͑ΔΑ͏ʹ͢ΔϓϩδΣΫτ ▸ "1*ΛநԽɾڞ௨Խ ▸ ൃੜ͢Δྫ֎Λڞ௨ྫ֎ʹม ▸ ϦϙδτϦΠϯλϑΣʔεͰ؆୯ʹॻ͚Δ ▸ +1"ɺ(FNpSFɺ.POHP%#ɺ3FEJTɺ $BTTBOESBͳͲଟ͘ͷٕज़ʹରԠ 13 (C) CASAREAL, Inc. All rights reserved.
+1"ͱʁ ▸ +BWB&&ͷҰ෦ͱͯ͠ඪ४Խ ͞ΕͨσʔλΞΫηεٕज़ͷ ༷ ▸ ࣮ମ+43ͱ͍͏จॻ ▸ ܾ·ͬͯΔͷΠϯλϑΣʔεɾ Ξϊςʔγϣϯɾྫ֎͕΄ͱΜ Ͳ 14 (C) CASAREAL, Inc. All rights reserved.
+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.
+1"ͷಛ ▸ %#ʹґଘ͠ͳ͍ૢ࡞͕هड़Ͱ͖Δ ▸ ΤϯςΟςΟΛςʔϒϧͱରͰ࡞Δ ▸ ʮ4&-&$5ͷ݁ՌͱରʯͰͳ͍ͷͰҙʂ ▸ ରଟͷΑ͏ͳϦϨʔγϣϯΛද͖ͰݱΔ 16 (C) CASAREAL, Inc. All rights reserved.
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.
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.
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
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.
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 21 (C) CASAREAL, Inc. All rights reserved.
+1"ͷΞʔΩςΫνϟ &OUJUZ .BOBHFS 'BDUPSZ ӬଓԽίϯςΩετ ੜ &OUJUZ &OUJUZ &OUJUZ &OUJUZ &OUJUZ .BOBHFS DB ΫϥΠΞϯτ ϓϩάϥϜ 22 (C) CASAREAL, Inc. All rights reserved.
ΤϯςΟςΟ ▸ جຊతʹςʔϒϧͱ ରͰ࡞Δ ▸ ʮ4&-&$5จͱରʯ Ͱͳ͍ʂ @Entity public class Product { @Id private Integer id; private String name; // setter/getter } ▸ ओΩʔϑΟʔϧυඞ ਢ 23 (C) CASAREAL, Inc. All rights reserved.
&OUJUZ.BOBHFS ▸ ΤϯςΟςΟͷʮঢ়ଶʯΛૢ࡞͢Δ૭ޱ ▸ ʮ$36%ૢ࡞ͷ૭ޱʯͰͳ͍ͷͰҙʂ ▸ ݁ࡧݕՌӬଓԽίϯςΩετʢޙड़ʣʹอଘ ▸ Πϯελϯε τϥϯβΫγϣϯ୯Ґ 24 (C) CASAREAL, Inc. All rights reserved.
&OUJUZ.BOBHFS'BDUPSZ ▸ &OUJUZ.BOBHFSΛੜ͢Δ ▸ ΠϯεϯλϯεΞϓϦέʔγϣϯͰͭͷΈ ʢʹγϯάϧτϯʣ 25 (C) CASAREAL, Inc. All rights reserved.
ӬଓԽίϯςΩετ ▸ ΤϯςΟςΟͷΩϟογϡͷΑ͏ͳͷ ▸ ΤϯςΟςΟ͕ӬଓԽίϯςΩετʹ·ؚΕ͍ͯΔ͔ Ͳ͏͔͕ඇৗʹॏཁ ▸ τϥϯβΫγϣϯ୯ҐͰ ੜɾഁ͞غΕΔ 26 (C) CASAREAL, Inc. All rights reserved.
ΤϯςΟςΟͷʮঢ়ଶʯ ঢ়ଶ NEW આ໌ new͞Ε͔ͨΓ MANAGED ӬଓԽίϯςΩετͰཧ͞Ε͍ͯΔ DETACHED ӬଓԽίϯςΩετ͔Β͞Ε͍ͯΔ REMOVED আ͕༧͞Ε͍ͯΔ 27 (C) CASAREAL, Inc. All rights reserved.
&OUJUZ.BOBHFSͷϝιου ϝιου આ໌ find() ओΩʔࡧݕ persist() 1݅ͷӬଓԽ remove() 1݅ͷআ merge() ӬଓԽίϯςΩετཧԼʹ͢Δ detach() ӬଓԽίϯςΩετཧ֎ʹ͢Δ flush() มߋΛDBө clear() ӬଓԽίϯςΩετΛΫϦΞ refresh() ΤϯςΟςΟͷ༰ΛDB࠷৽ʹ্ॻ͖ 28 (C) CASAREAL, Inc. All rights reserved.
˒ॏཁ˒ΤϯςΟςΟͷঢ়ଶભҠ ӬଓԽίϯςΩετ persist() /&8 ঢ়ଶ detach() clear() %&5"$)&% ঢ়ଶ ."/"(&% ঢ়ଶ remove() merge() detach() clear() 3&.07&% ঢ়ଶ find() JPQL flush() refresh() flush() 29 (C) CASAREAL, Inc. All rights reserved.
ओΩʔࡧݕ ▸ جຊతʹશϑΟʔϧυʹ૬͢Δྻ͕͞ࡧݕΕΔ 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
Ճ ▸ جຊతʹશϑΟʔϧυ͕*/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.
ߋ৽ ▸ ."/"(&%ঢ়ଶͷΤϯςΟςΟʹTFUUFSͰΛ ߋ৽ˠө // find()Ͱ͢ࡧݕΔͱMANAGEDঢ়ଶ Product product = em.find(Product.class, 1); // ͷߋ৽ product.setName("ߋ৽ޙͷ໊લ"); // ߋ৽ͷө(ίϛοτͰOK) em.flush(); 32 (C) CASAREAL, Inc. All rights reserved.
ߋ৽ 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.
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.
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.
NFSHF ͷམͱ݀͠ ▸ ҾΤϯςΟςΟͷίϐʔ͕࡞ΒΕɺͦΕ͕ Γ͔ͭ."/"(&%ঢ়ଶʹͳΔ ▸ Ҿࣗମ."/"(&%ঢ়ଶʹͳΒͳ͍ͷͰҙ Product mergedProduct = em.merge(product); // ΓMANAGEDঢ়ଶ assertTrue(em.contains(mergedProduct)); // Ҿͷঢ়ଶมΘΒͳ͍ assertFalse(em.contains(product)); 36 (C) CASAREAL, Inc. All rights reserved.
আ ▸ ."/"(&%ঢ়ଶͷΤϯςΟςΟΛSFNPWF ͢Δ // find()Ͱ͢ࡧݕΔͱMANAGEDঢ়ଶ Product product = em.find(Product.class, 30); // REMOVEDঢ়ଶʹ͢Δ em.remove(product); // ߋ৽ͷө(ίϛοτͰOK) em.flush(); 37 (C) CASAREAL, Inc. All rights reserved.
আ 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.
আͷΩϟϯηϧ ▸ 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.
·ͱΊ ▸ ΤϯςΟςΟͷʮঢ়ଶʯʹҙ ▸ pOE ͰओΩʔࡧݕ ▸ QFSTJTU ͰՃ ▸ ߋ৽pOE TFUUFSͰߋ৽ ▸ SFNPWF Ͱআ 40 (C) CASAREAL, Inc. All rights reserved.
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 41 (C) CASAREAL, Inc. All rights reserved.
+12-ͱʁ ▸ 42-ϥΠΫͳจ๏Ͱ4&-&$561%"5&%&-&5& Λهड़͢Δ ▸ */4&35ແ͍ ▸ %#ʹґଘ͠ͳ͍ΫΤϦ͕هड़Մೳ ▸ %#ݻ༗ػೳʹ͍ͭͯޙड़ 42 (C) CASAREAL, Inc. All rights reserved.
جຊతͳ͍ํ
▸ ୯Ұࡧݕ
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.
࣮ߦ͢Δͱ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
+12-ͷجຊจ๏ SELECT ࣝผม FROM ΤϯςΟςΟ໊ [AS] ࣝผม WHERE ࣝผม.ϓϩύςΟ໊ <= :ύϥϝʔλ໊ ▸ ࣝผมʹΤϯςΟςΟͷผ໊ʢඞਢʣ ▸ ΤϯςΟςΟ໊ϓϩύςΟ໊ɺ େจࣈখจࣈΛ۠ʹີݫผ͢ΔͷͰҙʂ ▸ 4&-&$5۠ผ͠ͳ͍͕େจࣈͷํ͕ؒҧ͑ʹ͍͘ 45 (C) CASAREAL, Inc. All rights reserved.
ίϯετϥΫλࣜ ▸ ඇΤϯςΟςΟͳΫϥεʹϚοϐϯάՄೳ ▸ ओͳ༻్ूܭ 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.
ͦͷଞͷ+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.
ͦͷଞͷ+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.
+12-Ͱग़དྷͳ͍͜ͱ ▸ */4&35 ▸ ू߹ԋࢉʢ6/*0/ɺ6/*0/"--ɺ */5&34&$5ɺ&9$&15ʣ ▸ '30.۟Ͱͷ෭߹ͤ 49 (C) CASAREAL, Inc. All rights reserved.
·ͱΊ ▸ 42-ϥΠΫͳจ๏Ͱ%#ʹґଘ͠ͳ͍ΫΤϦΛه ड़Մೳ ▸ Ͱ͖Δ͜ͱɾͰ͖ͳ͍͜ͱΛ͔ͬ͠Γҙࣝ͢ ͠ 50 (C) CASAREAL, Inc. All rights reserved.
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 51 (C) CASAREAL, Inc. All rights reserved.
ϦϨʔγϣϯΛද͢ݱΔ
▸ !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.
ϑΣονͱ ▸ ͋ΔΤϯςΟςΟΛಡΈࠐΜͩ࣌ɺϦϨʔγϣϯઌ ͷΤϯςΟςΟΛ͍ͭಡΈࠐΉ͔ ᶃ &"(&3ϑΣονɿଈ࣌ಡΈࠐΈ ▸ ಉ࣌ʹಡΈࠐΉʢ)JCFSOBUFͩͱ+0*/ɺ&DMJQTF-JOLͩͱ 4&-&$5จ͕ෳಉ࣌ʹൃߦʣ ᶄ -";:ϑΣονɿԆಡΈࠐΈ ▸ ඞཁʹͳͬͨ࣌ʢHFUUFS͕ݺΕͨ࣌ʣʹಡΈࠐΉ 53 (C) CASAREAL, Inc. All rights reserved.
ϑΣονͷࢦఆํ๏
▸ !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.
-";:ϑΣονͷྫ
@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.
-";:ϑΣονͷҙ ▸ ӬଓԽίϯςΩετഁޙغʢ㲈τϥϯβΫγϣϯ ऴྃޙʣʹϑΣον͢Δͱྫ֎͕ൃੜ͢Δ 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.
&"(&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.
ϑΣονجຊ-";:ʂ
▸ &"(&3ʹ͢Δͱෆཁͳσʔλ·ͰಡΈࠐΉ͔Β
▸ !0OF5P.BOZͰେྔͷσʔλΛಡΈࠐΉͱ͔ڪා
ʢʹ/ ʣ
▸ -";:͔Β&"(&3ʹಈతʹมߋ͍ͨ͠߹ɺ
+12-ͷ+0*/'&5$)จΛར༻͢Δ
58
(C) CASAREAL, Inc. All rights reserved.
/ ͕͜ىΔྫ
// 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.
୯ͳΔ+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
୯ͳΔ+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.
+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.
+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.
දࣔ͢Δͱɾɾɾ 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.
+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.
+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.
දࣔ͢Δͱɾɾɾ 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.
+12-ͷ%*45*/$5۟ ▸ 42-ͷ%*45*/$5۟ʴ-JTUͷॏෳআ͢Δ 68 (C) CASAREAL, Inc. All rights reserved.
ωετͨ͠+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.
ωετͨ͠+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.
·ͱΊ
▸ 'FUDI5ZQF-";:Λࢦఆ͠Α͏ʂ
▸ +0*/'&5$)จ %*45*/$5۟Ͱ/ Λղ
ܾͰ͖Δʂ
▸ ωετͨ͠+0*/'&5$))JCFSOBUFͷΈʂ
71
(C) CASAREAL, Inc. All rights reserved.
<খ>ࢭٳઈࢍɺࣾһืूதʂ ▸ ͖ͳٕज़ΛੈʹΊ͍ͨ ਓʂ ▸ 4QSJOHɺJ04ืٸ ▸ ։ൃҎ֎ͷखஈͰ ٕज़ʹؔΘΓ͍ͨਓʂ ▸ ຖఆ࣌ͰؼΓ͍ͨਓʂ https://www.casareal.co.jp/recruit/jobs/ ls_teacher.php ▸ ϗϫΠτ͢Ͱۀا 72 (C) CASAREAL, Inc. All rights reserved.
<খ>ࢭٳ৽ਓݚमʂ https://www.casareal.co.jp/ls/service/shinjinseminar/course01 ▸ Ϟμϯͳٕज़ ✕Ϟμϯͳڥ ✕ΞδϟΠϧ ▸ 3FBDU 4QSJOH#PPU (JU$*5%% Ϋϥυ %PDLFS 73 (C) CASAREAL, Inc. All rights reserved.
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 74 (C) CASAREAL, Inc. All rights reserved.
+1"Ͱར༻Ͱ͖Δ%#ݻ༗ػೳ ▸ ωΠςΟϒ42- ▸ +12-͔Βͷ%#ݻ༗ؔͼݺग़͠ ▸ ετΞυϓϩγʔδϟ ▸ ΠϯσοΫε 75 (C) CASAREAL, Inc. All rights reserved.
ωΠςΟϒ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.
ΤϯςΟςΟʹϚοϐϯά͠ͳ͍ํ๏
▸ ΓΛ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.
!4RM3FTVMU4FU.BQQJOH ▸ ωΠςΟϒ42-ͷ݁ՌΛҙͷΫϥεʹϚοϐ ϯάग़དྷΔ ▸ ͨͩ͠ɺهड़͔ͳΓʢ࣍ϖʔδʣ 78 (C) CASAREAL, Inc. All rights reserved.
!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
!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.
+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.
ετΞυϓϩγʔδϟ ▸ DSFBUF4UPSFE1SPDFEVSF2VFSZ StoredProcedureQuery query = em.createStoredProcedureQuery("ϓϩγʔδϟ໊", ΤϯςΟςΟ.class); query.setParameter("ύϥϝʔλ໊", ύϥϝʔλ); List<ΤϯςΟςΟ> list = (List<ΤϯςΟςΟ>) query.getResultList(); ※ετΞυϓϩγʔδϟʹৄ͘͠ͳ͍ͨΊචऀະ͢Ͱূݕɻͥͻࢼͯ͠ϑΟʔυόοΫ͍ͩ͘͞ʂ 82 (C) CASAREAL, Inc. All rights reserved.
ΠϯσοΫε ▸ !*OEFY @Entity @Index(members = {"name"}) public class Product { … } ※ΠϯσοΫεʹৄ͘͠ͳ͍ͨΊචऀະ͢Ͱূݕɻͥͻࢼͯ͠ϑΟʔυόοΫ͍ͩ͘͞ʂ ※@IndicesΛར༻ͯ͠ෳͷ@IndexΛࢦఆՄೳ 83 (C) CASAREAL, Inc. All rights reserved.
%#ݻ༗ػೳͷར༻࠷ऴखஈ ▸ ʮ%#͕มΘͬͯίʔυͷมߋ͕ඞཁͳ͍ʯ ͱ͍͏+1"ͷϝϦοτ͕ࣦΘΕΔ ▸ ར༻ͤ͟ΔΛಘͳ͍ՕॴɺυΩϡϝϯτͱ͔ ʹ͓͍ͯͨ͠΄͏͕Α͍ ▸ ͦ͏͠ͳ͍ͱʹޙ%#ΛҠ২͢Δ͜ͱʹͳͬͨͱ ͖େม 84 (C) CASAREAL, Inc. All rights reserved.
·ͱΊ ▸ %#ݻ༗ػೳͦͦ͑͜͜Δ ▸ ͨͩ͠ɺ΄Ͳ΄Ͳʹ͠·͠ΐ͏ 85 (C) CASAREAL, Inc. All rights reserved.
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 86 (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 87 (C) CASAREAL, Inc. All rights reserved.
4QSJOH%BUB+1"ͷศརػೳ ▸ 3FQPTJUPSZΠϯλϑΣʔε࡞Δ͚ͩ ▸ ϝιου໊͔Βͷ+12-ࣗಈੜ ▸ ڞ௨ඇνΣοΫྫ֎ͷม ▸ ࠪରԠ ▸ %BUFBOE5JNF"1* +43 ରԠ 88 (C) CASAREAL, Inc. All rights reserved.
3FQPTJUPSZΠϯλϑΣʔε ▸ +QB3FQPTJUPSZΛܧঝͨ͠ΠϯλϑΣʔεΛ࡞ Δ͚ͩͰɺجຊతͳ$36%ॲཧ࡞ྃ public interface ProductRepository extends JpaRepository<Product, Integer> { // தۭͰOK } ※@RepositoryΞϊςʔγϣϯඞཁ͋Γ·ͤΜ 89 (C) CASAREAL, Inc. All rights reserved.
+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.
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
!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.
!.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.
!-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
!&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.
ϝιου໊͔Βͷ+12-ࣗಈੜ
▸ ϧʔϧʹैͬͯϝιου໊Λهड़͢Εɺ
!2VFSZͰ+12-Λهड़͢Δඞཁͳ͠
public interface ProductRepository
extends JpaRepository<Product, Integer> {
// nameʹҾͷจࣈྻ͕·ؚΕ͍ͯΔͷΛ͢ࡧݕΔ
List<Product> findByNameContaining(String keyword);
}
96
(C) CASAREAL, Inc. All rights reserved.
ϝιου໊͔Βͷ+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.
ϝιου໊ʹ͑ΔΩʔϫʔυʢҰ෦ʣ ▸ "OE 0S #FUXFFO -FTT5IBO -FTT5IBO&RVBM -JLF /PU-JLF 4UBSUJOH8JUI &OEJOH8JUI $POUBJOJOH 0SEFS#Z ʜ ▸ ৄࡉϦϑΝϨϯεࢀর ▸ IUUQTEPDTTQSJOHJPTQSJOHEBUBKQBEPDT DVSSFOUSFGFSFODFIUNMKQBRVFSZ NFUIPETRVFSZDSFBUJPO 98 (C) CASAREAL, Inc. All rights reserved.
ڞ௨ඇνΣοΫྫ֎ͷม ▸ +1"Ͱൃੜͨ͠ྫ֎%BUB"DDFTT&YDFQUJPO ͷαϒΫϥεʹม͞ΕΔ ▸ ͲΜͳσʔλΞΫηεٕज़Λ͍ͬͯΔ͔Λ্Ґʹ ҙࣝͤ͞ͳ͍ %BUB"DDFTT&YDFQUJPO %BUB*OUFHSJUZ7JPMBUJPO &YDFQUJPO #BE4RM(SBNNBS &YDFQUJPO ɾɾɾ 99 (C) CASAREAL, Inc. All rights reserved.
ࠪରԠ ▸ ࡞ऀɺ࡞࣌ɺߋ৽ऀɺߋ৽࣌Λࣗಈత ʹ%#ʹอଘͰ͖Δ @Entity public class Hoge { @CreatedBy String createdUser; @CreatedDate LocalDateTime createdDate; @LastModifiedBy String modifiedUser; @LastModifiedDate LocalDateTime lastModifiedDate; } 100 (C) CASAREAL, Inc. All rights reserved.
ࠪରԠʹඞཁͳઃఆ
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.
ࠪରԠʹඞཁͳઃఆʢ͖ͭͮʣ
▸ 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.
%BUFBOE5JNF"1* +43 ରԠ ▸ +TS+QB$POWFSUFSTΫϥε ▸ -PDBM%BUFΫϥεͳͲͷ"UUSJCVUF$POWFSUFS͕͋Β͔ ͡Ίఏ͞ڙΕ͍ͯΔ ▸ TFU1BDLBHF5P4DBO ͳͲͰ PSHTQSJOHGSBNFXPSLEBUBKQBDPOWFSUUISFFUFOύ οέʔδΛࢦఆ͢Ε0, ▸ 4QSJOH#PPUͰ"VUP$POpHVSBUJPOରʹͳ͍ͬͯΔ 103 (C) CASAREAL, Inc. All rights reserved.
ͦͷଞͷػೳʢࢼͯ͠ͳ͍ʣ ▸ !1SPDFEVSF QSPDFEVSF/BNFϓϩγʔδϟ ໊ ▸ &YBNQMF.BUDIFSʹΑΔλΠϓηʔϑهड़ ▸ +QB$POUFYU 104 (C) CASAREAL, Inc. All rights reserved.
·ͱΊ ▸ ศརͰ͢Ͷʂ ▸ 3FQPTJUPSZΠϯλϑΣʔε࡞Δ͚ͩ ▸ ϝιου໊͔Βͷ+12-ࣗಈੜ ▸ ڞ௨ඇνΣοΫྫ֎ͷม ▸ ࠪରԠ ▸ %BUFBOE5JNF"1* +43 ରԠ 105 (C) CASAREAL, Inc. All rights reserved.
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 106 (C) CASAREAL, Inc. All rights reserved.
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
0QFO*O7JFXΛΦϑʹ͢Δํ๏ ▸ BQQMJDBUJPOQSPQFSUJFTʹԼهͷهड़ΛՃ͢Δ ͱɺ௨ৗͷ4QSJOHͱಉ͡ʢτϥϯβΫγϣϯ୯ ҐʣʹͳΔ spring.jpa.open-in-view=false 108 (C) CASAREAL, Inc. All rights reserved.
/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
/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.
!&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
ᶃ +1"4QSJOH%BUB+1"ͱʁ ᶄ +1"ΤϯςΟςΟͷঢ়ଶཧ ᶅ +BWB1FSTJTUFODF2VFSZ-BOHVBHF +12- ᶆ ϦϨʔγϣϯͱ/ ରࡦ ᶇ %#ݻ༗ػೳͷར༻ ᶈ 4QSJOH%BUB+1"ͷར༻ ᶉ 4QSJOH#PPUར༻࣌ͷҙ ᶊ +1"ʮ͑Δʯͷ͔ʁ 112 (C) CASAREAL, Inc. All rights reserved.
+1"ʮ͑Δʯ͔ʁ ▸ ʮ͑Δɻͨͩ͠ਖ਼͍͕ࣝ͠ඞཁɺ͔ͭγνϡ ΤʔγϣϯΛબͿʯͱ͍͏ͷ͕ݸਓతͳҙͰݟ ͢ 113 (C) CASAREAL, Inc. All rights reserved.
+1"Λ͍͍ͬͯ݅ ᶃ %#Λ৽ʹنઃ͖ͰܭΔ ᶄ ू߹ԋࢉ'30.۟Ͱͷ෭߹ͤͳͲɺ ෳࡶͳ42-ཁ݅తʹগͳ͍ ᶅ ʮύʔϑΣΫτ+BWB&&ʯΛಡഁͨ͠ਓ͕ ϓϩδΣΫτʹਓҎ্͍Δ ͭͰͯ·Βͳ͍߲͕͋Εɺ ଞͷ03ϚούʔΛͬͨํ͕͍͍͔😅 114 (C) CASAREAL, Inc. All rights reserved.
ඞಡॻ੶ ▸ 4QSJOHపఈೖ ▸ ֓ཁΛΔͳΒ͜ͷຊʂ ▸ ύʔϑΣΫτ+BWB&& ▸ +1"Λৄࡉ·ͰΕΔʂ ▸ 4QSJOH%BUB+1"ϓϩάϥϛ ϯάೖ ▸ 4QSJOH%BUB+1"ͷ֤ػೳΛֶ शʂ 115 (C) CASAREAL, Inc. All rights reserved.
ඞಡࢿྉ ▸ ·Δʂ+1"ʢॳֶऀ͚ϥΠτ൛ʣ !TVLF@NBTB ▸ IUUQTXXXTMJEFTIBSFOFUNBTBUPTIJUBEBKQB ▸ ·Δʂ+1" !NBLJOH͞Μ ▸ IUUQTXXXTMJEFTIBSFOFUNBLJOHYKQB ▸ +1"ͷಉ࣮࣌ߦ੍ͱޚϩοΫ !TVLF@NBTB ▸ IUUQTXXXTMJEFTIBSFOFUNBTBUPTIJUBEBKQBDDDSKKVH DDD ▸ !PQFOHM@͞Μͷ2JJUBࣄهҰ࿈ ▸ IUUQRJJUBDPNPQFOHMJUFNTGGBFDC 116 (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 117 (C) CASAREAL, Inc. All rights reserved.
&OKPZ4QSJOH%BUB+1" ▸ ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ 118 (C) CASAREAL, Inc. All rights reserved.