(C) CASAREAL, Inc. All rights reserved. ओΩʔݕࡧ 21 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͕ ൃߦ͞ΕΔ
(C) CASAREAL, Inc. All rights reserved. আ 29 select … from product product0_ where product0_.id=? binding parameter [1] as [INTEGER] - [30] delete from product where id=? binding parameter [1] as [INTEGER] - [30]
(C) CASAREAL, Inc. All rights reserved. جຊతͳ͍ํ 34 TypedQuery query = em.createQuery( "SELECT p FROM Product p WHERE p.id = :id", Product.class); query.setParameter("id", 1); Product product = query.getSingleResult(); TypedQuery query = em.createQuery( "SELECT p FROM Product p WHERE p.id <= :maxId" + " ORDER BY p.id", Product.class); query.setParameter("maxId", 5); List list = query.getResultList(); ▸ ୯Ұݕࡧ ▸ ෳݕࡧ
(C) CASAREAL, Inc. All rights reserved. ࣮ߦ͢Δͱ42-ʹ༁͞ΕΔ 35 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ͷྫʣ
(C) CASAREAL, Inc. All rights reserved. ϦϨʔγϣϯΛදݱ͢Δ ▸ !0OF5P0OF ▸ !0OF5P.BOZ ▸ !.BOZ5P0OF ▸ !.BOZ5P.BOZ 40 @Entity public class Product { … @ManyToOne @JoinColumn(name="vendor_id") private Vendor vendor; } @Entity public class Vendor { … }
(C) CASAREAL, Inc. All rights reserved. -";:ϑΣονͷྫ 42 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
(C) CASAREAL, Inc. All rights reserved. -";:ϑΣονͷҙ ▸ ӬଓԽίϯςΩετഁغޙʢ㲈τϥϯβΫγϣϯ ऴྃޙʣʹϑΣον͢Δͱྫ֎͕ൃੜ͢Δ 43 em.getTransaction().begin(); Product p = em.find(Product.class, 1); em.getTransaction().rollback(); Vendor v = p.getVendor(); String n = v.getName(); // ͜͜Ͱྫ֎ൃੜ
(C) CASAREAL, Inc. All rights reserved. &"(&3ϑΣονͷྫ 44 @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
(C) CASAREAL, Inc. All rights reserved. /͕ى͜Δྫ 46 // order_summaryͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ List 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); } }
(C) CASAREAL, Inc. All rights reserved. +0*/'&5$)จͰղܾʂʁ ▸ -";:ʹͳ͍ͬͯΔϦϨʔγϣϯΛಈతʹ&"(&3 ʹมߋͯ͘͠ΕΔ 47 // order_summaryͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ List list = em.createQuery( "SELECT os FROM OrderSummary os" + " JOIN FETCH os.orderDetailList" + , OrderSummary.class) .getResultList();
(C) CASAREAL, Inc. All rights reserved. +0*/'&5$)จͰղܾʂʁ 48 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ͯ͠ શྻऔಘ
(C) CASAREAL, Inc. All rights reserved. +0*/'&5$)%*45*/$5Ͱղܾʂ 50 // order_summaryͷSELECT͕ൃߦ͞ΕΔʢ1ճʣ List list = em.createQuery( "SELECT DISTINCT os FROM OrderSummary os" + " JOIN FETCH os.orderDetailList" + , OrderSummary.class) .getResultList();
(C) CASAREAL, Inc. All rights reserved. +0*/'&5$)%*45*/$5Ͱղܾʂ 51 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ͷ݁Ռ ͜ͷ߹ಉ͡