06-spring-boot-data.html 120KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 1.5.5">
  8. <title>Spring Boot Data</title>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
  10. <style>
  11. /* Asciidoctor default stylesheet | MIT License | http://asciidoctor.org */
  12. /* Remove comment around @import statement below when using as a custom stylesheet */
  13. /*@import "https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700";*/
  14. article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block}
  15. audio,canvas,video{display:inline-block}
  16. audio:not([controls]){display:none;height:0}
  17. [hidden],template{display:none}
  18. script{display:none!important}
  19. html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}
  20. a{background:transparent}
  21. a:focus{outline:thin dotted}
  22. a:active,a:hover{outline:0}
  23. h1{font-size:2em;margin:.67em 0}
  24. abbr[title]{border-bottom:1px dotted}
  25. b,strong{font-weight:bold}
  26. dfn{font-style:italic}
  27. hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}
  28. mark{background:#ff0;color:#000}
  29. code,kbd,pre,samp{font-family:monospace;font-size:1em}
  30. pre{white-space:pre-wrap}
  31. q{quotes:"\201C" "\201D" "\2018" "\2019"}
  32. small{font-size:80%}
  33. sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}
  34. sup{top:-.5em}
  35. sub{bottom:-.25em}
  36. img{border:0}
  37. svg:not(:root){overflow:hidden}
  38. figure{margin:0}
  39. fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}
  40. legend{border:0;padding:0}
  41. button,input,select,textarea{font-family:inherit;font-size:100%;margin:0}
  42. button,input{line-height:normal}
  43. button,select{text-transform:none}
  44. button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}
  45. button[disabled],html input[disabled]{cursor:default}
  46. input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0}
  47. input[type="search"]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}
  48. input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none}
  49. button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}
  50. textarea{overflow:auto;vertical-align:top}
  51. table{border-collapse:collapse;border-spacing:0}
  52. *,*:before,*:after{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}
  53. html,body{font-size:100%}
  54. body{background:#fff;color:rgba(0,0,0,.8);padding:0;margin:0;font-family:"Noto Serif","DejaVu Serif",serif;font-weight:400;font-style:normal;line-height:1;position:relative;cursor:auto;tab-size:4;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased}
  55. a:hover{cursor:pointer}
  56. img,object,embed{max-width:100%;height:auto}
  57. object,embed{height:100%}
  58. img{-ms-interpolation-mode:bicubic}
  59. .left{float:left!important}
  60. .right{float:right!important}
  61. .text-left{text-align:left!important}
  62. .text-right{text-align:right!important}
  63. .text-center{text-align:center!important}
  64. .text-justify{text-align:justify!important}
  65. .hide{display:none}
  66. img,object,svg{display:inline-block;vertical-align:middle}
  67. textarea{height:auto;min-height:50px}
  68. select{width:100%}
  69. .center{margin-left:auto;margin-right:auto}
  70. .spread{width:100%}
  71. p.lead,.paragraph.lead>p,#preamble>.sectionbody>.paragraph:first-of-type p{font-size:1.21875em;line-height:1.6}
  72. .subheader,.admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{line-height:1.45;color:#7a2518;font-weight:400;margin-top:0;margin-bottom:.25em}
  73. div,dl,dt,dd,ul,ol,li,h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6,pre,form,p,blockquote,th,td{margin:0;padding:0;direction:ltr}
  74. a{color:#2156a5;text-decoration:underline;line-height:inherit}
  75. a:hover,a:focus{color:#1d4b8f}
  76. a img{border:none}
  77. p{font-family:inherit;font-weight:400;font-size:1em;line-height:1.6;margin-bottom:1.25em;text-rendering:optimizeLegibility}
  78. p aside{font-size:.875em;line-height:1.35;font-style:italic}
  79. h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{font-family:"Open Sans","DejaVu Sans",sans-serif;font-weight:300;font-style:normal;color:#ba3925;text-rendering:optimizeLegibility;margin-top:1em;margin-bottom:.5em;line-height:1.0125em}
  80. h1 small,h2 small,h3 small,#toctitle small,.sidebarblock>.content>.title small,h4 small,h5 small,h6 small{font-size:60%;color:#e99b8f;line-height:0}
  81. h1{font-size:2.125em}
  82. h2{font-size:1.6875em}
  83. h3,#toctitle,.sidebarblock>.content>.title{font-size:1.375em}
  84. h4,h5{font-size:1.125em}
  85. h6{font-size:1em}
  86. hr{border:solid #ddddd8;border-width:1px 0 0;clear:both;margin:1.25em 0 1.1875em;height:0}
  87. em,i{font-style:italic;line-height:inherit}
  88. strong,b{font-weight:bold;line-height:inherit}
  89. small{font-size:60%;line-height:inherit}
  90. code{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;color:rgba(0,0,0,.9)}
  91. ul,ol,dl{font-size:1em;line-height:1.6;margin-bottom:1.25em;list-style-position:outside;font-family:inherit}
  92. ul,ol,ul.no-bullet,ol.no-bullet{margin-left:1.5em}
  93. ul li ul,ul li ol{margin-left:1.25em;margin-bottom:0;font-size:1em}
  94. ul.square li ul,ul.circle li ul,ul.disc li ul{list-style:inherit}
  95. ul.square{list-style-type:square}
  96. ul.circle{list-style-type:circle}
  97. ul.disc{list-style-type:disc}
  98. ul.no-bullet{list-style:none}
  99. ol li ul,ol li ol{margin-left:1.25em;margin-bottom:0}
  100. dl dt{margin-bottom:.3125em;font-weight:bold}
  101. dl dd{margin-bottom:1.25em}
  102. abbr,acronym{text-transform:uppercase;font-size:90%;color:rgba(0,0,0,.8);border-bottom:1px dotted #ddd;cursor:help}
  103. abbr{text-transform:none}
  104. blockquote{margin:0 0 1.25em;padding:.5625em 1.25em 0 1.1875em;border-left:1px solid #ddd}
  105. blockquote cite{display:block;font-size:.9375em;color:rgba(0,0,0,.6)}
  106. blockquote cite:before{content:"\2014 \0020"}
  107. blockquote cite a,blockquote cite a:visited{color:rgba(0,0,0,.6)}
  108. blockquote,blockquote p{line-height:1.6;color:rgba(0,0,0,.85)}
  109. @media only screen and (min-width:768px){h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2}
  110. h1{font-size:2.75em}
  111. h2{font-size:2.3125em}
  112. h3,#toctitle,.sidebarblock>.content>.title{font-size:1.6875em}
  113. h4{font-size:1.4375em}}
  114. table{background:#fff;margin-bottom:1.25em;border:solid 1px #dedede}
  115. table thead,table tfoot{background:#f7f8f7;font-weight:bold}
  116. table thead tr th,table thead tr td,table tfoot tr th,table tfoot tr td{padding:.5em .625em .625em;font-size:inherit;color:rgba(0,0,0,.8);text-align:left}
  117. table tr th,table tr td{padding:.5625em .625em;font-size:inherit;color:rgba(0,0,0,.8)}
  118. table tr.even,table tr.alt,table tr:nth-of-type(even){background:#f8f8f7}
  119. table thead tr th,table tfoot tr th,table tbody tr td,table tr td,table tfoot tr td{display:table-cell;line-height:1.6}
  120. h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2;word-spacing:-.05em}
  121. h1 strong,h2 strong,h3 strong,#toctitle strong,.sidebarblock>.content>.title strong,h4 strong,h5 strong,h6 strong{font-weight:400}
  122. .clearfix:before,.clearfix:after,.float-group:before,.float-group:after{content:" ";display:table}
  123. .clearfix:after,.float-group:after{clear:both}
  124. *:not(pre)>code{font-size:.9375em;font-style:normal!important;letter-spacing:0;padding:.1em .5ex;word-spacing:-.15em;background-color:#f7f7f8;-webkit-border-radius:4px;border-radius:4px;line-height:1.45;text-rendering:optimizeSpeed;word-wrap:break-word}
  125. *:not(pre)>code.nobreak{word-wrap:normal}
  126. *:not(pre)>code.nowrap{white-space:nowrap}
  127. pre,pre>code{line-height:1.45;color:rgba(0,0,0,.9);font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;text-rendering:optimizeSpeed}
  128. em em{font-style:normal}
  129. strong strong{font-weight:400}
  130. .keyseq{color:rgba(51,51,51,.8)}
  131. kbd{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background-color:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
  132. .keyseq kbd:first-child{margin-left:0}
  133. .keyseq kbd:last-child{margin-right:0}
  134. .menuseq,.menu{color:rgba(0,0,0,.8)}
  135. b.button:before,b.button:after{position:relative;top:-1px;font-weight:400}
  136. b.button:before{content:"[";padding:0 3px 0 2px}
  137. b.button:after{content:"]";padding:0 2px 0 3px}
  138. p a>code:hover{color:rgba(0,0,0,.9)}
  139. #header,#content,#footnotes,#footer{width:100%;margin-left:auto;margin-right:auto;margin-top:0;margin-bottom:0;max-width:62.5em;*zoom:1;position:relative;padding-left:.9375em;padding-right:.9375em}
  140. #header:before,#header:after,#content:before,#content:after,#footnotes:before,#footnotes:after,#footer:before,#footer:after{content:" ";display:table}
  141. #header:after,#content:after,#footnotes:after,#footer:after{clear:both}
  142. #content{margin-top:1.25em}
  143. #content:before{content:none}
  144. #header>h1:first-child{color:rgba(0,0,0,.85);margin-top:2.25rem;margin-bottom:0}
  145. #header>h1:first-child+#toc{margin-top:8px;border-top:1px solid #ddddd8}
  146. #header>h1:only-child,body.toc2 #header>h1:nth-last-child(2){border-bottom:1px solid #ddddd8;padding-bottom:8px}
  147. #header .details{border-bottom:1px solid #ddddd8;line-height:1.45;padding-top:.25em;padding-bottom:.25em;padding-left:.25em;color:rgba(0,0,0,.6);display:-ms-flexbox;display:-webkit-flex;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap}
  148. #header .details span:first-child{margin-left:-.125em}
  149. #header .details span.email a{color:rgba(0,0,0,.85)}
  150. #header .details br{display:none}
  151. #header .details br+span:before{content:"\00a0\2013\00a0"}
  152. #header .details br+span.author:before{content:"\00a0\22c5\00a0";color:rgba(0,0,0,.85)}
  153. #header .details br+span#revremark:before{content:"\00a0|\00a0"}
  154. #header #revnumber{text-transform:capitalize}
  155. #header #revnumber:after{content:"\00a0"}
  156. #content>h1:first-child:not([class]){color:rgba(0,0,0,.85);border-bottom:1px solid #ddddd8;padding-bottom:8px;margin-top:0;padding-top:1rem;margin-bottom:1.25rem}
  157. #toc{border-bottom:1px solid #efefed;padding-bottom:.5em}
  158. #toc>ul{margin-left:.125em}
  159. #toc ul.sectlevel0>li>a{font-style:italic}
  160. #toc ul.sectlevel0 ul.sectlevel1{margin:.5em 0}
  161. #toc ul{font-family:"Open Sans","DejaVu Sans",sans-serif;list-style-type:none}
  162. #toc li{line-height:1.3334;margin-top:.3334em}
  163. #toc a{text-decoration:none}
  164. #toc a:active{text-decoration:underline}
  165. #toctitle{color:#7a2518;font-size:1.2em}
  166. @media only screen and (min-width:768px){#toctitle{font-size:1.375em}
  167. body.toc2{padding-left:15em;padding-right:0}
  168. #toc.toc2{margin-top:0!important;background-color:#f8f8f7;position:fixed;width:15em;left:0;top:0;border-right:1px solid #efefed;border-top-width:0!important;border-bottom-width:0!important;z-index:1000;padding:1.25em 1em;height:100%;overflow:auto}
  169. #toc.toc2 #toctitle{margin-top:0;margin-bottom:.8rem;font-size:1.2em}
  170. #toc.toc2>ul{font-size:.9em;margin-bottom:0}
  171. #toc.toc2 ul ul{margin-left:0;padding-left:1em}
  172. #toc.toc2 ul.sectlevel0 ul.sectlevel1{padding-left:0;margin-top:.5em;margin-bottom:.5em}
  173. body.toc2.toc-right{padding-left:0;padding-right:15em}
  174. body.toc2.toc-right #toc.toc2{border-right-width:0;border-left:1px solid #efefed;left:auto;right:0}}
  175. @media only screen and (min-width:1280px){body.toc2{padding-left:20em;padding-right:0}
  176. #toc.toc2{width:20em}
  177. #toc.toc2 #toctitle{font-size:1.375em}
  178. #toc.toc2>ul{font-size:.95em}
  179. #toc.toc2 ul ul{padding-left:1.25em}
  180. body.toc2.toc-right{padding-left:0;padding-right:20em}}
  181. #content #toc{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
  182. #content #toc>:first-child{margin-top:0}
  183. #content #toc>:last-child{margin-bottom:0}
  184. #footer{max-width:100%;background-color:rgba(0,0,0,.8);padding:1.25em}
  185. #footer-text{color:rgba(255,255,255,.8);line-height:1.44}
  186. .sect1{padding-bottom:.625em}
  187. @media only screen and (min-width:768px){.sect1{padding-bottom:1.25em}}
  188. .sect1+.sect1{border-top:1px solid #efefed}
  189. #content h1>a.anchor,h2>a.anchor,h3>a.anchor,#toctitle>a.anchor,.sidebarblock>.content>.title>a.anchor,h4>a.anchor,h5>a.anchor,h6>a.anchor{position:absolute;z-index:1001;width:1.5ex;margin-left:-1.5ex;display:block;text-decoration:none!important;visibility:hidden;text-align:center;font-weight:400}
  190. #content h1>a.anchor:before,h2>a.anchor:before,h3>a.anchor:before,#toctitle>a.anchor:before,.sidebarblock>.content>.title>a.anchor:before,h4>a.anchor:before,h5>a.anchor:before,h6>a.anchor:before{content:"\00A7";font-size:.85em;display:block;padding-top:.1em}
  191. #content h1:hover>a.anchor,#content h1>a.anchor:hover,h2:hover>a.anchor,h2>a.anchor:hover,h3:hover>a.anchor,#toctitle:hover>a.anchor,.sidebarblock>.content>.title:hover>a.anchor,h3>a.anchor:hover,#toctitle>a.anchor:hover,.sidebarblock>.content>.title>a.anchor:hover,h4:hover>a.anchor,h4>a.anchor:hover,h5:hover>a.anchor,h5>a.anchor:hover,h6:hover>a.anchor,h6>a.anchor:hover{visibility:visible}
  192. #content h1>a.link,h2>a.link,h3>a.link,#toctitle>a.link,.sidebarblock>.content>.title>a.link,h4>a.link,h5>a.link,h6>a.link{color:#ba3925;text-decoration:none}
  193. #content h1>a.link:hover,h2>a.link:hover,h3>a.link:hover,#toctitle>a.link:hover,.sidebarblock>.content>.title>a.link:hover,h4>a.link:hover,h5>a.link:hover,h6>a.link:hover{color:#a53221}
  194. .audioblock,.imageblock,.literalblock,.listingblock,.stemblock,.videoblock{margin-bottom:1.25em}
  195. .admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{text-rendering:optimizeLegibility;text-align:left;font-family:"Noto Serif","DejaVu Serif",serif;font-size:1rem;font-style:italic}
  196. table.tableblock>caption.title{white-space:nowrap;overflow:visible;max-width:0}
  197. .paragraph.lead>p,#preamble>.sectionbody>.paragraph:first-of-type p{color:rgba(0,0,0,.85)}
  198. table.tableblock #preamble>.sectionbody>.paragraph:first-of-type p{font-size:inherit}
  199. .admonitionblock>table{border-collapse:separate;border:0;background:none;width:100%}
  200. .admonitionblock>table td.icon{text-align:center;width:80px}
  201. .admonitionblock>table td.icon img{max-width:none}
  202. .admonitionblock>table td.icon .title{font-weight:bold;font-family:"Open Sans","DejaVu Sans",sans-serif;text-transform:uppercase}
  203. .admonitionblock>table td.content{padding-left:1.125em;padding-right:1.25em;border-left:1px solid #ddddd8;color:rgba(0,0,0,.6)}
  204. .admonitionblock>table td.content>:last-child>:last-child{margin-bottom:0}
  205. .exampleblock>.content{border-style:solid;border-width:1px;border-color:#e6e6e6;margin-bottom:1.25em;padding:1.25em;background:#fff;-webkit-border-radius:4px;border-radius:4px}
  206. .exampleblock>.content>:first-child{margin-top:0}
  207. .exampleblock>.content>:last-child{margin-bottom:0}
  208. .sidebarblock{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
  209. .sidebarblock>:first-child{margin-top:0}
  210. .sidebarblock>:last-child{margin-bottom:0}
  211. .sidebarblock>.content>.title{color:#7a2518;margin-top:0;text-align:center}
  212. .exampleblock>.content>:last-child>:last-child,.exampleblock>.content .olist>ol>li:last-child>:last-child,.exampleblock>.content .ulist>ul>li:last-child>:last-child,.exampleblock>.content .qlist>ol>li:last-child>:last-child,.sidebarblock>.content>:last-child>:last-child,.sidebarblock>.content .olist>ol>li:last-child>:last-child,.sidebarblock>.content .ulist>ul>li:last-child>:last-child,.sidebarblock>.content .qlist>ol>li:last-child>:last-child{margin-bottom:0}
  213. .literalblock pre,.listingblock pre:not(.highlight),.listingblock pre[class="highlight"],.listingblock pre[class^="highlight "],.listingblock pre.CodeRay,.listingblock pre.prettyprint{background:#f7f7f8}
  214. .sidebarblock .literalblock pre,.sidebarblock .listingblock pre:not(.highlight),.sidebarblock .listingblock pre[class="highlight"],.sidebarblock .listingblock pre[class^="highlight "],.sidebarblock .listingblock pre.CodeRay,.sidebarblock .listingblock pre.prettyprint{background:#f2f1f1}
  215. .literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{-webkit-border-radius:4px;border-radius:4px;word-wrap:break-word;padding:1em;font-size:.8125em}
  216. .literalblock pre.nowrap,.literalblock pre[class].nowrap,.listingblock pre.nowrap,.listingblock pre[class].nowrap{overflow-x:auto;white-space:pre;word-wrap:normal}
  217. @media only screen and (min-width:768px){.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{font-size:.90625em}}
  218. @media only screen and (min-width:1280px){.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{font-size:1em}}
  219. .literalblock.output pre{color:#f7f7f8;background-color:rgba(0,0,0,.9)}
  220. .listingblock pre.highlightjs{padding:0}
  221. .listingblock pre.highlightjs>code{padding:1em;-webkit-border-radius:4px;border-radius:4px}
  222. .listingblock pre.prettyprint{border-width:0}
  223. .listingblock>.content{position:relative}
  224. .listingblock code[data-lang]:before{display:none;content:attr(data-lang);position:absolute;font-size:.75em;top:.425rem;right:.5rem;line-height:1;text-transform:uppercase;color:#999}
  225. .listingblock:hover code[data-lang]:before{display:block}
  226. .listingblock.terminal pre .command:before{content:attr(data-prompt);padding-right:.5em;color:#999}
  227. .listingblock.terminal pre .command:not([data-prompt]):before{content:"$"}
  228. table.pyhltable{border-collapse:separate;border:0;margin-bottom:0;background:none}
  229. table.pyhltable td{vertical-align:top;padding-top:0;padding-bottom:0;line-height:1.45}
  230. table.pyhltable td.code{padding-left:.75em;padding-right:0}
  231. pre.pygments .lineno,table.pyhltable td:not(.code){color:#999;padding-left:0;padding-right:.5em;border-right:1px solid #ddddd8}
  232. pre.pygments .lineno{display:inline-block;margin-right:.25em}
  233. table.pyhltable .linenodiv{background:none!important;padding-right:0!important}
  234. .quoteblock{margin:0 1em 1.25em 1.5em;display:table}
  235. .quoteblock>.title{margin-left:-1.5em;margin-bottom:.75em}
  236. .quoteblock blockquote,.quoteblock blockquote p{color:rgba(0,0,0,.85);font-size:1.15rem;line-height:1.75;word-spacing:.1em;letter-spacing:0;font-style:italic;text-align:justify}
  237. .quoteblock blockquote{margin:0;padding:0;border:0}
  238. .quoteblock blockquote:before{content:"\201c";float:left;font-size:2.75em;font-weight:bold;line-height:.6em;margin-left:-.6em;color:#7a2518;text-shadow:0 1px 2px rgba(0,0,0,.1)}
  239. .quoteblock blockquote>.paragraph:last-child p{margin-bottom:0}
  240. .quoteblock .attribution{margin-top:.5em;margin-right:.5ex;text-align:right}
  241. .quoteblock .quoteblock{margin-left:0;margin-right:0;padding:.5em 0;border-left:3px solid rgba(0,0,0,.6)}
  242. .quoteblock .quoteblock blockquote{padding:0 0 0 .75em}
  243. .quoteblock .quoteblock blockquote:before{display:none}
  244. .verseblock{margin:0 1em 1.25em 1em}
  245. .verseblock pre{font-family:"Open Sans","DejaVu Sans",sans;font-size:1.15rem;color:rgba(0,0,0,.85);font-weight:300;text-rendering:optimizeLegibility}
  246. .verseblock pre strong{font-weight:400}
  247. .verseblock .attribution{margin-top:1.25rem;margin-left:.5ex}
  248. .quoteblock .attribution,.verseblock .attribution{font-size:.9375em;line-height:1.45;font-style:italic}
  249. .quoteblock .attribution br,.verseblock .attribution br{display:none}
  250. .quoteblock .attribution cite,.verseblock .attribution cite{display:block;letter-spacing:-.025em;color:rgba(0,0,0,.6)}
  251. .quoteblock.abstract{margin:0 0 1.25em 0;display:block}
  252. .quoteblock.abstract blockquote,.quoteblock.abstract blockquote p{text-align:left;word-spacing:0}
  253. .quoteblock.abstract blockquote:before,.quoteblock.abstract blockquote p:first-of-type:before{display:none}
  254. table.tableblock{max-width:100%;border-collapse:separate}
  255. table.tableblock td>.paragraph:last-child p>p:last-child,table.tableblock th>p:last-child,table.tableblock td>p:last-child{margin-bottom:0}
  256. table.tableblock,th.tableblock,td.tableblock{border:0 solid #dedede}
  257. table.grid-all th.tableblock,table.grid-all td.tableblock{border-width:0 1px 1px 0}
  258. table.grid-all tfoot>tr>th.tableblock,table.grid-all tfoot>tr>td.tableblock{border-width:1px 1px 0 0}
  259. table.grid-cols th.tableblock,table.grid-cols td.tableblock{border-width:0 1px 0 0}
  260. table.grid-all *>tr>.tableblock:last-child,table.grid-cols *>tr>.tableblock:last-child{border-right-width:0}
  261. table.grid-rows th.tableblock,table.grid-rows td.tableblock{border-width:0 0 1px 0}
  262. table.grid-all tbody>tr:last-child>th.tableblock,table.grid-all tbody>tr:last-child>td.tableblock,table.grid-all thead:last-child>tr>th.tableblock,table.grid-rows tbody>tr:last-child>th.tableblock,table.grid-rows tbody>tr:last-child>td.tableblock,table.grid-rows thead:last-child>tr>th.tableblock{border-bottom-width:0}
  263. table.grid-rows tfoot>tr>th.tableblock,table.grid-rows tfoot>tr>td.tableblock{border-width:1px 0 0 0}
  264. table.frame-all{border-width:1px}
  265. table.frame-sides{border-width:0 1px}
  266. table.frame-topbot{border-width:1px 0}
  267. th.halign-left,td.halign-left{text-align:left}
  268. th.halign-right,td.halign-right{text-align:right}
  269. th.halign-center,td.halign-center{text-align:center}
  270. th.valign-top,td.valign-top{vertical-align:top}
  271. th.valign-bottom,td.valign-bottom{vertical-align:bottom}
  272. th.valign-middle,td.valign-middle{vertical-align:middle}
  273. table thead th,table tfoot th{font-weight:bold}
  274. tbody tr th{display:table-cell;line-height:1.6;background:#f7f8f7}
  275. tbody tr th,tbody tr th p,tfoot tr th,tfoot tr th p{color:rgba(0,0,0,.8);font-weight:bold}
  276. p.tableblock>code:only-child{background:none;padding:0}
  277. p.tableblock{font-size:1em}
  278. td>div.verse{white-space:pre}
  279. ol{margin-left:1.75em}
  280. ul li ol{margin-left:1.5em}
  281. dl dd{margin-left:1.125em}
  282. dl dd:last-child,dl dd:last-child>:last-child{margin-bottom:0}
  283. ol>li p,ul>li p,ul dd,ol dd,.olist .olist,.ulist .ulist,.ulist .olist,.olist .ulist{margin-bottom:.625em}
  284. ul.unstyled,ol.unnumbered,ul.checklist,ul.none{list-style-type:none}
  285. ul.unstyled,ol.unnumbered,ul.checklist{margin-left:.625em}
  286. ul.checklist li>p:first-child>.fa-square-o:first-child,ul.checklist li>p:first-child>.fa-check-square-o:first-child{width:1em;font-size:.85em}
  287. ul.checklist li>p:first-child>input[type="checkbox"]:first-child{width:1em;position:relative;top:1px}
  288. ul.inline{margin:0 auto .625em auto;margin-left:-1.375em;margin-right:0;padding:0;list-style:none;overflow:hidden}
  289. ul.inline>li{list-style:none;float:left;margin-left:1.375em;display:block}
  290. ul.inline>li>*{display:block}
  291. .unstyled dl dt{font-weight:400;font-style:normal}
  292. ol.arabic{list-style-type:decimal}
  293. ol.decimal{list-style-type:decimal-leading-zero}
  294. ol.loweralpha{list-style-type:lower-alpha}
  295. ol.upperalpha{list-style-type:upper-alpha}
  296. ol.lowerroman{list-style-type:lower-roman}
  297. ol.upperroman{list-style-type:upper-roman}
  298. ol.lowergreek{list-style-type:lower-greek}
  299. .hdlist>table,.colist>table{border:0;background:none}
  300. .hdlist>table>tbody>tr,.colist>table>tbody>tr{background:none}
  301. td.hdlist1,td.hdlist2{vertical-align:top;padding:0 .625em}
  302. td.hdlist1{font-weight:bold;padding-bottom:1.25em}
  303. .literalblock+.colist,.listingblock+.colist{margin-top:-.5em}
  304. .colist>table tr>td:first-of-type{padding:0 .75em;line-height:1}
  305. .colist>table tr>td:last-of-type{padding:.25em 0}
  306. .thumb,.th{line-height:0;display:inline-block;border:solid 4px #fff;-webkit-box-shadow:0 0 0 1px #ddd;box-shadow:0 0 0 1px #ddd}
  307. .imageblock.left,.imageblock[style*="float: left"]{margin:.25em .625em 1.25em 0}
  308. .imageblock.right,.imageblock[style*="float: right"]{margin:.25em 0 1.25em .625em}
  309. .imageblock>.title{margin-bottom:0}
  310. .imageblock.thumb,.imageblock.th{border-width:6px}
  311. .imageblock.thumb>.title,.imageblock.th>.title{padding:0 .125em}
  312. .image.left,.image.right{margin-top:.25em;margin-bottom:.25em;display:inline-block;line-height:0}
  313. .image.left{margin-right:.625em}
  314. .image.right{margin-left:.625em}
  315. a.image{text-decoration:none;display:inline-block}
  316. a.image object{pointer-events:none}
  317. sup.footnote,sup.footnoteref{font-size:.875em;position:static;vertical-align:super}
  318. sup.footnote a,sup.footnoteref a{text-decoration:none}
  319. sup.footnote a:active,sup.footnoteref a:active{text-decoration:underline}
  320. #footnotes{padding-top:.75em;padding-bottom:.75em;margin-bottom:.625em}
  321. #footnotes hr{width:20%;min-width:6.25em;margin:-.25em 0 .75em 0;border-width:1px 0 0 0}
  322. #footnotes .footnote{padding:0 .375em 0 .225em;line-height:1.3334;font-size:.875em;margin-left:1.2em;text-indent:-1.05em;margin-bottom:.2em}
  323. #footnotes .footnote a:first-of-type{font-weight:bold;text-decoration:none}
  324. #footnotes .footnote:last-of-type{margin-bottom:0}
  325. #content #footnotes{margin-top:-.625em;margin-bottom:0;padding:.75em 0}
  326. .gist .file-data>table{border:0;background:#fff;width:100%;margin-bottom:0}
  327. .gist .file-data>table td.line-data{width:99%}
  328. div.unbreakable{page-break-inside:avoid}
  329. .big{font-size:larger}
  330. .small{font-size:smaller}
  331. .underline{text-decoration:underline}
  332. .overline{text-decoration:overline}
  333. .line-through{text-decoration:line-through}
  334. .aqua{color:#00bfbf}
  335. .aqua-background{background-color:#00fafa}
  336. .black{color:#000}
  337. .black-background{background-color:#000}
  338. .blue{color:#0000bf}
  339. .blue-background{background-color:#0000fa}
  340. .fuchsia{color:#bf00bf}
  341. .fuchsia-background{background-color:#fa00fa}
  342. .gray{color:#606060}
  343. .gray-background{background-color:#7d7d7d}
  344. .green{color:#006000}
  345. .green-background{background-color:#007d00}
  346. .lime{color:#00bf00}
  347. .lime-background{background-color:#00fa00}
  348. .maroon{color:#600000}
  349. .maroon-background{background-color:#7d0000}
  350. .navy{color:#000060}
  351. .navy-background{background-color:#00007d}
  352. .olive{color:#606000}
  353. .olive-background{background-color:#7d7d00}
  354. .purple{color:#600060}
  355. .purple-background{background-color:#7d007d}
  356. .red{color:#bf0000}
  357. .red-background{background-color:#fa0000}
  358. .silver{color:#909090}
  359. .silver-background{background-color:#bcbcbc}
  360. .teal{color:#006060}
  361. .teal-background{background-color:#007d7d}
  362. .white{color:#bfbfbf}
  363. .white-background{background-color:#fafafa}
  364. .yellow{color:#bfbf00}
  365. .yellow-background{background-color:#fafa00}
  366. span.icon>.fa{cursor:default}
  367. .admonitionblock td.icon [class^="fa icon-"]{font-size:2.5em;text-shadow:1px 1px 2px rgba(0,0,0,.5);cursor:default}
  368. .admonitionblock td.icon .icon-note:before{content:"\f05a";color:#19407c}
  369. .admonitionblock td.icon .icon-tip:before{content:"\f0eb";text-shadow:1px 1px 2px rgba(155,155,0,.8);color:#111}
  370. .admonitionblock td.icon .icon-warning:before{content:"\f071";color:#bf6900}
  371. .admonitionblock td.icon .icon-caution:before{content:"\f06d";color:#bf3400}
  372. .admonitionblock td.icon .icon-important:before{content:"\f06a";color:#bf0000}
  373. .conum[data-value]{display:inline-block;color:#fff!important;background-color:rgba(0,0,0,.8);-webkit-border-radius:100px;border-radius:100px;text-align:center;font-size:.75em;width:1.67em;height:1.67em;line-height:1.67em;font-family:"Open Sans","DejaVu Sans",sans-serif;font-style:normal;font-weight:bold}
  374. .conum[data-value] *{color:#fff!important}
  375. .conum[data-value]+b{display:none}
  376. .conum[data-value]:after{content:attr(data-value)}
  377. pre .conum[data-value]{position:relative;top:-.125em}
  378. b.conum *{color:inherit!important}
  379. .conum:not([data-value]):empty{display:none}
  380. dt,th.tableblock,td.content,div.footnote{text-rendering:optimizeLegibility}
  381. h1,h2,p,td.content,span.alt{letter-spacing:-.01em}
  382. p strong,td.content strong,div.footnote strong{letter-spacing:-.005em}
  383. p,blockquote,dt,td.content,span.alt{font-size:1.0625rem}
  384. p{margin-bottom:1.25rem}
  385. .sidebarblock p,.sidebarblock dt,.sidebarblock td.content,p.tableblock{font-size:1em}
  386. .exampleblock>.content{background-color:#fffef7;border-color:#e0e0dc;-webkit-box-shadow:0 1px 4px #e0e0dc;box-shadow:0 1px 4px #e0e0dc}
  387. .print-only{display:none!important}
  388. @media print{@page{margin:1.25cm .75cm}
  389. *{-webkit-box-shadow:none!important;box-shadow:none!important;text-shadow:none!important}
  390. a{color:inherit!important;text-decoration:underline!important}
  391. a.bare,a[href^="#"],a[href^="mailto:"]{text-decoration:none!important}
  392. a[href^="http:"]:not(.bare):after,a[href^="https:"]:not(.bare):after{content:"(" attr(href) ")";display:inline-block;font-size:.875em;padding-left:.25em}
  393. abbr[title]:after{content:" (" attr(title) ")"}
  394. pre,blockquote,tr,img,object,svg{page-break-inside:avoid}
  395. thead{display:table-header-group}
  396. svg{max-width:100%}
  397. p,blockquote,dt,td.content{font-size:1em;orphans:3;widows:3}
  398. h2,h3,#toctitle,.sidebarblock>.content>.title{page-break-after:avoid}
  399. #toc,.sidebarblock,.exampleblock>.content{background:none!important}
  400. #toc{border-bottom:1px solid #ddddd8!important;padding-bottom:0!important}
  401. .sect1{padding-bottom:0!important}
  402. .sect1+.sect1{border:0!important}
  403. #header>h1:first-child{margin-top:1.25rem}
  404. body.book #header{text-align:center}
  405. body.book #header>h1:first-child{border:0!important;margin:2.5em 0 1em 0}
  406. body.book #header .details{border:0!important;display:block;padding:0!important}
  407. body.book #header .details span:first-child{margin-left:0!important}
  408. body.book #header .details br{display:block}
  409. body.book #header .details br+span:before{content:none!important}
  410. body.book #toc{border:0!important;text-align:left!important;padding:0!important;margin:0!important}
  411. body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-break-before:always}
  412. .listingblock code[data-lang]:before{display:block}
  413. #footer{background:none!important;padding:0 .9375em}
  414. #footer-text{color:rgba(0,0,0,.6)!important;font-size:.9em}
  415. .hide-on-print{display:none!important}
  416. .print-only{display:block!important}
  417. .hide-for-print{display:none!important}
  418. .show-for-print{display:inherit!important}}
  419. </style>
  420. <style>
  421. /* Stylesheet for CodeRay to match GitHub theme | MIT License | http://foundation.zurb.com */
  422. /*pre.CodeRay {background-color:#f7f7f8;}*/
  423. .CodeRay .line-numbers{border-right:1px solid #d8d8d8;padding:0 0.5em 0 .25em}
  424. .CodeRay span.line-numbers{display:inline-block;margin-right:.5em;color:rgba(0,0,0,.3)}
  425. .CodeRay .line-numbers strong{color:rgba(0,0,0,.4)}
  426. table.CodeRay{border-collapse:separate;border-spacing:0;margin-bottom:0;border:0;background:none}
  427. table.CodeRay td{vertical-align: top;line-height:1.45}
  428. table.CodeRay td.line-numbers{text-align:right}
  429. table.CodeRay td.line-numbers>pre{padding:0;color:rgba(0,0,0,.3)}
  430. table.CodeRay td.code{padding:0 0 0 .5em}
  431. table.CodeRay td.code>pre{padding:0}
  432. .CodeRay .debug{color:#fff !important;background:#000080 !important}
  433. .CodeRay .annotation{color:#007}
  434. .CodeRay .attribute-name{color:#000080}
  435. .CodeRay .attribute-value{color:#700}
  436. .CodeRay .binary{color:#509}
  437. .CodeRay .comment{color:#998;font-style:italic}
  438. .CodeRay .char{color:#04d}
  439. .CodeRay .char .content{color:#04d}
  440. .CodeRay .char .delimiter{color:#039}
  441. .CodeRay .class{color:#458;font-weight:bold}
  442. .CodeRay .complex{color:#a08}
  443. .CodeRay .constant,.CodeRay .predefined-constant{color:#008080}
  444. .CodeRay .color{color:#099}
  445. .CodeRay .class-variable{color:#369}
  446. .CodeRay .decorator{color:#b0b}
  447. .CodeRay .definition{color:#099}
  448. .CodeRay .delimiter{color:#000}
  449. .CodeRay .doc{color:#970}
  450. .CodeRay .doctype{color:#34b}
  451. .CodeRay .doc-string{color:#d42}
  452. .CodeRay .escape{color:#666}
  453. .CodeRay .entity{color:#800}
  454. .CodeRay .error{color:#808}
  455. .CodeRay .exception{color:inherit}
  456. .CodeRay .filename{color:#099}
  457. .CodeRay .function{color:#900;font-weight:bold}
  458. .CodeRay .global-variable{color:#008080}
  459. .CodeRay .hex{color:#058}
  460. .CodeRay .integer,.CodeRay .float{color:#099}
  461. .CodeRay .include{color:#555}
  462. .CodeRay .inline{color:#000}
  463. .CodeRay .inline .inline{background:#ccc}
  464. .CodeRay .inline .inline .inline{background:#bbb}
  465. .CodeRay .inline .inline-delimiter{color:#d14}
  466. .CodeRay .inline-delimiter{color:#d14}
  467. .CodeRay .important{color:#555;font-weight:bold}
  468. .CodeRay .interpreted{color:#b2b}
  469. .CodeRay .instance-variable{color:#008080}
  470. .CodeRay .label{color:#970}
  471. .CodeRay .local-variable{color:#963}
  472. .CodeRay .octal{color:#40e}
  473. .CodeRay .predefined{color:#369}
  474. .CodeRay .preprocessor{color:#579}
  475. .CodeRay .pseudo-class{color:#555}
  476. .CodeRay .directive{font-weight:bold}
  477. .CodeRay .type{font-weight:bold}
  478. .CodeRay .predefined-type{color:inherit}
  479. .CodeRay .reserved,.CodeRay .keyword {color:#000;font-weight:bold}
  480. .CodeRay .key{color:#808}
  481. .CodeRay .key .delimiter{color:#606}
  482. .CodeRay .key .char{color:#80f}
  483. .CodeRay .value{color:#088}
  484. .CodeRay .regexp .delimiter{color:#808}
  485. .CodeRay .regexp .content{color:#808}
  486. .CodeRay .regexp .modifier{color:#808}
  487. .CodeRay .regexp .char{color:#d14}
  488. .CodeRay .regexp .function{color:#404;font-weight:bold}
  489. .CodeRay .string{color:#d20}
  490. .CodeRay .string .string .string{background:#ffd0d0}
  491. .CodeRay .string .content{color:#d14}
  492. .CodeRay .string .char{color:#d14}
  493. .CodeRay .string .delimiter{color:#d14}
  494. .CodeRay .shell{color:#d14}
  495. .CodeRay .shell .delimiter{color:#d14}
  496. .CodeRay .symbol{color:#990073}
  497. .CodeRay .symbol .content{color:#a60}
  498. .CodeRay .symbol .delimiter{color:#630}
  499. .CodeRay .tag{color:#008080}
  500. .CodeRay .tag-special{color:#d70}
  501. .CodeRay .variable{color:#036}
  502. .CodeRay .insert{background:#afa}
  503. .CodeRay .delete{background:#faa}
  504. .CodeRay .change{color:#aaf;background:#007}
  505. .CodeRay .head{color:#f8f;background:#505}
  506. .CodeRay .insert .insert{color:#080}
  507. .CodeRay .delete .delete{color:#800}
  508. .CodeRay .change .change{color:#66f}
  509. .CodeRay .head .head{color:#f4f}
  510. </style>
  511. <link rel="stylesheet"
  512. href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
  513. integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
  514. crossorigin="anonymous">
  515. <link rel="stylesheet"
  516. href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
  517. integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
  518. crossorigin="anonymous">
  519. <style>
  520. .c2cb{
  521. position: absolute; left: 0; top: 0;
  522. cursor: pointer
  523. }
  524. .c2cb:focus {
  525. outline: none;
  526. }
  527. body {
  528. font-size: 18px;
  529. }
  530. p {
  531. font-size: 18px;
  532. }
  533. td.content {
  534. font-size:2.0625rem;
  535. }
  536. div.title {
  537. font-size:2.0625rem;
  538. }
  539. </style>
  540. <script src="js/jquery-3.2.1.min.js"></script>
  541. <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js"></script>
  542. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  543. <script>
  544. $(function() {
  545. var button = $("<div style='padding: 10px;'><button data-toggle='tooltip' placement='right' data-trigger='click' title='Copied!' class='c2cb glyphicon glyphicon-copy'></button></div>");
  546. $(".listingblock > .content pre").prepend(button);
  547. $('[data-toggle="tooltip"]').tooltip();
  548. $('[data-toggle="tooltip"]').mouseout(function() {
  549. $(this).tooltip('hide');
  550. $(this).trigger('blur');
  551. });
  552. var clipboard = new Clipboard('.c2cb', {
  553. target: function(c2cb) {
  554. return $(c2cb).parent().siblings("code")[0];
  555. }
  556. });
  557. clipboard.on('success', function(e) {
  558. e.clearSelection();
  559. });
  560. });
  561. </script>
  562. </head>
  563. <body class="article toc2 toc-left">
  564. <div id="header">
  565. <h1><a id="spring-boot-data"></a>Spring Boot Data</h1>
  566. <div id="toc" class="toc2">
  567. <div id="toctitle">Labs</div>
  568. <ul class="sectlevel1">
  569. <li><a href="#_code_snippet_manager_jdbc">Code Snippet Manager JDBC</a>
  570. <ul class="sectlevel2">
  571. <li><a href="#_challenges">Challenges</a></li>
  572. </ul>
  573. </li>
  574. <li><a href="#_code_snippet_manager_jpa">Code Snippet Manager JPA</a>
  575. <ul class="sectlevel2">
  576. <li><a href="#_challenges_2">Challenges</a></li>
  577. </ul>
  578. </li>
  579. <li><a href="#_code_snippet_manager_jpa_rest">Code Snippet Manager JPA REST</a>
  580. <ul class="sectlevel2">
  581. <li><a href="#_challenges_3">Challenges</a></li>
  582. </ul>
  583. </li>
  584. <li><a href="#_homework">HOMEWORK</a></li>
  585. </ul>
  586. </div>
  587. </div>
  588. <div id="content">
  589. <div id="preamble">
  590. <div class="sectionbody">
  591. <div class="paragraph">
  592. <p>Get familiar with the <strong>Spring Data</strong> and the <strong>Spring Boot Data</strong> features.</p>
  593. </div>
  594. <div class="paragraph">
  595. <p><strong>Time:</strong> 60 minutes (20 minutes per section).</p>
  596. </div>
  597. <!-- toc disabled -->
  598. </div>
  599. </div>
  600. <div class="sect1">
  601. <h2 id="_code_snippet_manager_jdbc">Code Snippet Manager JDBC</h2>
  602. <div class="sectionbody">
  603. <div class="paragraph">
  604. <p>You will continue with the <strong>Code Snippet Manager</strong> code, but this time using a persistence engine. You will reuse the code from previous labs.</p>
  605. </div>
  606. <div class="olist arabic">
  607. <ol class="arabic">
  608. <li>
  609. <p>Open a browser and hit the url: <a href="http://start.spring.io" class="bare" target="_blank">http://start.spring.io</a></p>
  610. </li>
  611. <li>
  612. <p>Click the <strong><em>Switch to the full version</em></strong> link.</p>
  613. </li>
  614. <li>
  615. <p>Fill out the <strong>Code Snippet Manager JDBC</strong> Project metadata with (See Figure 1.0):</p>
  616. <table class="tableblock frame-all grid-all spread">
  617. <caption class="title">Table 1. Code Snippet Manager JDBC App - metadata</caption>
  618. <colgroup>
  619. <col style="width: 16.6666%;">
  620. <col style="width: 83.3334%;">
  621. </colgroup>
  622. <thead>
  623. <tr>
  624. <th class="tableblock halign-left valign-top">Property</th>
  625. <th class="tableblock halign-left valign-top">Value</th>
  626. </tr>
  627. </thead>
  628. <tbody>
  629. <tr>
  630. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Group</strong>:</p></td>
  631. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>io.pivotal.workshop</em></p></td>
  632. </tr>
  633. <tr>
  634. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Artifact</strong>:</p></td>
  635. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>code-snippet-manager-jdbc</em></p></td>
  636. </tr>
  637. <tr>
  638. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Name</strong>:</p></td>
  639. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>code-snippet-manager-jdbc</em></p></td>
  640. </tr>
  641. <tr>
  642. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Package Name</strong>:</p></td>
  643. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>io.pivotal.workshop.snippet</em></p></td>
  644. </tr>
  645. <tr>
  646. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Dependencies</strong>:</p></td>
  647. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>Web, DevTools, Groovy Templates, JDBC, H2, MySQL</em></p></td>
  648. </tr>
  649. <tr>
  650. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Spring Boot</strong>:</p></td>
  651. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>2.0.0.M7</em></p></td>
  652. </tr>
  653. </tbody>
  654. </table>
  655. <div class="paragraph">
  656. <div class="title">Figure 1.0: Spring Initializr - <a href="http://start.spring.io/" class="bare" target="_blank">http://start.spring.io/</a></div>
  657. <p><span class="image"><img src="images/06-spring-boot-data-01.png" alt="SpringInitializr" title="Spring Initializr"></span></p>
  658. </div>
  659. <div class="admonitionblock tip">
  660. <table>
  661. <tr>
  662. <td class="icon">
  663. <div class="title">Tip</div>
  664. </td>
  665. <td class="content">
  666. You can choose either <strong>Maven</strong> or <strong>Gradle</strong> project types.
  667. </td>
  668. </tr>
  669. </table>
  670. </div>
  671. </li>
  672. <li>
  673. <p>Type <strong>Web</strong>, <strong>DevTools</strong>, <strong>Groovy Templates</strong>, <strong>JDBC</strong>, <strong>H2</strong>, and <strong>MySQL</strong> in the <strong><em>Dependencies</em></strong> field and press Enter.</p>
  674. </li>
  675. <li>
  676. <p>Click the <strong><em>Generate Project</em></strong> button.</p>
  677. </li>
  678. <li>
  679. <p>Unzip the file in any directory you want.</p>
  680. </li>
  681. <li>
  682. <p>Import your project in any IDE you want.</p>
  683. </li>
  684. <li>
  685. <p>Copy all the packages (with code) into the new project.</p>
  686. </li>
  687. <li>
  688. <p>The classes in <strong>io.pivotal.workshop.snippet.domain</strong>: <strong><em>Code</em></strong>, <strong><em>Language</em></strong> and <strong><em>Snippet</em></strong> should be the same.</p>
  689. </li>
  690. <li>
  691. <p>Create a new class <strong>CrossSnippetLanguageCode</strong> in the <strong>domain</strong> package:</p>
  692. <div class="listingblock">
  693. <div class="content">
  694. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.domain</span>;
  695. <span class="directive">public</span> <span class="type">class</span> <span class="class">CrossSnippetLanguageCode</span> {
  696. <span class="directive">private</span> <span class="predefined-type">String</span> snippetId;
  697. <span class="directive">private</span> <span class="predefined-type">String</span> languageId;
  698. <span class="directive">private</span> <span class="predefined-type">String</span> codeId;
  699. <span class="directive">public</span> CrossSnippetLanguageCode() {
  700. }
  701. <span class="directive">public</span> CrossSnippetLanguageCode(<span class="predefined-type">String</span> snippetId, <span class="predefined-type">String</span> languageId, <span class="predefined-type">String</span> codeId) {
  702. <span class="local-variable">this</span>.snippetId = snippetId;
  703. <span class="local-variable">this</span>.languageId = languageId;
  704. <span class="local-variable">this</span>.codeId = codeId;
  705. }
  706. <span class="directive">public</span> <span class="predefined-type">String</span> getSnippetId() {
  707. <span class="keyword">return</span> snippetId;
  708. }
  709. <span class="directive">public</span> <span class="type">void</span> setSnippetId(<span class="predefined-type">String</span> snippetId) {
  710. <span class="local-variable">this</span>.snippetId = snippetId;
  711. }
  712. <span class="directive">public</span> <span class="predefined-type">String</span> getLanguageId() {
  713. <span class="keyword">return</span> languageId;
  714. }
  715. <span class="directive">public</span> <span class="type">void</span> setLanguageId(<span class="predefined-type">String</span> languageId) {
  716. <span class="local-variable">this</span>.languageId = languageId;
  717. }
  718. <span class="directive">public</span> <span class="predefined-type">String</span> getCodeId() {
  719. <span class="keyword">return</span> codeId;
  720. }
  721. <span class="directive">public</span> <span class="type">void</span> setCodeId(<span class="predefined-type">String</span> codeId) {
  722. <span class="local-variable">this</span>.codeId = codeId;
  723. }
  724. }</code></pre>
  725. </div>
  726. </div>
  727. <div class="paragraph">
  728. <p>this class will hold the relationship between the <strong>Code</strong> and <strong>Language</strong> within the <strong>Snippet</strong> class.</p>
  729. </div>
  730. </li>
  731. <li>
  732. <p>Because you will reuse the <strong>SimpleRepository</strong> interface, is necessary modify all the implementations. Modify the code accordingly:</p>
  733. <div class="listingblock">
  734. <div class="title">io.pivotal.workshop.snippet.repository.LanguageRepository.java</div>
  735. <div class="content">
  736. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository</span>;
  737. <span class="keyword">import</span> <span class="include">java.sql.PreparedStatement</span>;
  738. <span class="keyword">import</span> <span class="include">java.util.Collection</span>;
  739. <span class="keyword">import</span> <span class="include">org.springframework.beans.factory.annotation.Autowired</span>;
  740. <span class="keyword">import</span> <span class="include">org.springframework.dao.EmptyResultDataAccessException</span>;
  741. <span class="keyword">import</span> <span class="include">org.springframework.jdbc.core.JdbcTemplate</span>;
  742. <span class="keyword">import</span> <span class="include">org.springframework.stereotype.Repository</span>;
  743. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Language</span>;
  744. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.repository.mapper.LanguageRowMapper</span>;
  745. <span class="annotation">@Repository</span>
  746. <span class="directive">public</span> <span class="type">class</span> <span class="class">LanguageRepository</span> <span class="directive">implements</span> SimpleRepository&lt;Language&gt;{
  747. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_FIND_ALL = <span class="string"><span class="delimiter">&quot;</span><span class="content">select * from language</span><span class="delimiter">&quot;</span></span>;
  748. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_FIND_ONE = <span class="string"><span class="delimiter">&quot;</span><span class="content">select * from language where name = ?</span><span class="delimiter">&quot;</span></span>;
  749. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_INSERT = <span class="string"><span class="delimiter">&quot;</span><span class="content">insert into language(id,name,syntax) values(?,?,?)</span><span class="delimiter">&quot;</span></span>;
  750. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_UPDATE = <span class="string"><span class="delimiter">&quot;</span><span class="content">update language set name = ?, syntax = ? where id = ?</span><span class="delimiter">&quot;</span></span>;
  751. <span class="directive">private</span> JdbcTemplate jdbcTemplate;
  752. <span class="annotation">@Autowired</span>
  753. <span class="directive">public</span> LanguageRepository(JdbcTemplate jdbcTemplate){
  754. <span class="local-variable">this</span>.jdbcTemplate = jdbcTemplate;
  755. }
  756. <span class="annotation">@Override</span>
  757. <span class="directive">public</span> <span class="predefined-type">Iterable</span>&lt;Language&gt; findAll(){
  758. <span class="keyword">return</span> <span class="local-variable">this</span>.jdbcTemplate.query(SQL_FIND_ALL, <span class="keyword">new</span> LanguageRowMapper());
  759. }
  760. <span class="annotation">@Override</span>
  761. <span class="directive">public</span> <span class="type">void</span> saveAll(<span class="predefined-type">Collection</span>&lt;Language&gt; languages){
  762. languages.forEach( lang -&gt; saveAll(lang) );
  763. }
  764. <span class="annotation">@Override</span>
  765. <span class="directive">public</span> Language findById(<span class="predefined-type">String</span> name) {
  766. <span class="keyword">try</span> {
  767. <span class="keyword">return</span> <span class="local-variable">this</span>.jdbcTemplate.queryForObject(SQL_FIND_ONE, <span class="keyword">new</span> <span class="predefined-type">Object</span><span class="type">[]</span>{name}, <span class="keyword">new</span> LanguageRowMapper());
  768. } <span class="keyword">catch</span> (EmptyResultDataAccessException ex){
  769. <span class="keyword">return</span> <span class="predefined-constant">null</span>;
  770. }
  771. }
  772. <span class="annotation">@Override</span>
  773. <span class="directive">public</span> Language saveAll(Language item) {
  774. <span class="keyword">assert</span> item.getName() != <span class="predefined-constant">null</span>;
  775. Language language = <span class="local-variable">this</span>.findById(item.getName());
  776. <span class="keyword">if</span>(language == <span class="predefined-constant">null</span>) {
  777. <span class="local-variable">this</span>.jdbcTemplate.update( psc -&gt; {
  778. <span class="predefined-type">PreparedStatement</span> ps = psc.prepareStatement(SQL_INSERT);
  779. ps.setString(<span class="integer">1</span>, item.getId());
  780. ps.setString(<span class="integer">2</span>, item.getName());
  781. ps.setString(<span class="integer">3</span>, item.getSyntax());
  782. <span class="keyword">return</span> ps;
  783. });
  784. <span class="keyword">return</span> item;
  785. }<span class="keyword">else</span> {
  786. <span class="local-variable">this</span>.jdbcTemplate.update( psc -&gt; {
  787. <span class="predefined-type">PreparedStatement</span> ps = psc.prepareStatement(SQL_UPDATE);
  788. ps.setString(<span class="integer">1</span>, item.getName());
  789. ps.setString(<span class="integer">2</span>, item.getSyntax());
  790. ps.setString(<span class="integer">3</span>, item.getId());
  791. <span class="keyword">return</span> ps;
  792. });
  793. <span class="keyword">return</span> language;
  794. }
  795. }
  796. }</code></pre>
  797. </div>
  798. </div>
  799. <div class="listingblock">
  800. <div class="title">io.pivotal.workshop.snippet.repository.CodeRepository.java</div>
  801. <div class="content">
  802. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository</span>;
  803. <span class="keyword">import</span> <span class="include">java.sql.PreparedStatement</span>;
  804. <span class="keyword">import</span> <span class="include">java.util.Collection</span>;
  805. <span class="keyword">import</span> <span class="include">java.util.List</span>;
  806. <span class="keyword">import</span> <span class="include">org.springframework.beans.factory.annotation.Autowired</span>;
  807. <span class="keyword">import</span> <span class="include">org.springframework.dao.EmptyResultDataAccessException</span>;
  808. <span class="keyword">import</span> <span class="include">org.springframework.jdbc.core.JdbcTemplate</span>;
  809. <span class="keyword">import</span> <span class="include">org.springframework.stereotype.Repository</span>;
  810. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Code</span>;
  811. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.repository.mapper.CodeRowMapper</span>;
  812. <span class="annotation">@Repository</span>
  813. <span class="directive">public</span> <span class="type">class</span> <span class="class">CodeRepository</span> <span class="directive">implements</span> SimpleRepository&lt;Code&gt; {
  814. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_FIND_ALL = <span class="string"><span class="delimiter">&quot;</span><span class="content">select * from code</span><span class="delimiter">&quot;</span></span>;
  815. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_FIND_ONE = <span class="string"><span class="delimiter">&quot;</span><span class="content">select * from code where id = ?</span><span class="delimiter">&quot;</span></span>;
  816. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_INSERT = <span class="string"><span class="delimiter">&quot;</span><span class="content">insert into code(id,source) values(?,?)</span><span class="delimiter">&quot;</span></span>;
  817. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_UPDATE = <span class="string"><span class="delimiter">&quot;</span><span class="content">update code set source = ? where id = ?</span><span class="delimiter">&quot;</span></span>;
  818. <span class="directive">private</span> JdbcTemplate jdbcTemplate;
  819. <span class="annotation">@Autowired</span>
  820. <span class="directive">public</span> CodeRepository(JdbcTemplate jdbcTemplate) {
  821. <span class="local-variable">this</span>.jdbcTemplate = jdbcTemplate;
  822. }
  823. <span class="annotation">@Override</span>
  824. <span class="directive">public</span> <span class="predefined-type">List</span>&lt;Code&gt; findAll() {
  825. <span class="keyword">return</span> <span class="local-variable">this</span>.jdbcTemplate.query(SQL_FIND_ALL, <span class="keyword">new</span> CodeRowMapper());
  826. }
  827. <span class="annotation">@Override</span>
  828. <span class="directive">public</span> <span class="type">void</span> saveAll(<span class="predefined-type">Collection</span>&lt;Code&gt; items) {
  829. items.forEach(code -&gt; <span class="local-variable">this</span>.saveAll(code));
  830. }
  831. <span class="annotation">@Override</span>
  832. <span class="directive">public</span> Code findById(<span class="predefined-type">String</span> id) {
  833. <span class="keyword">try</span> {
  834. <span class="keyword">return</span> <span class="local-variable">this</span>.jdbcTemplate.queryForObject(SQL_FIND_ONE, <span class="keyword">new</span> <span class="predefined-type">Object</span><span class="type">[]</span> { id }, <span class="keyword">new</span> CodeRowMapper());
  835. } <span class="keyword">catch</span> (EmptyResultDataAccessException ex) {
  836. <span class="keyword">return</span> <span class="predefined-constant">null</span>;
  837. }
  838. }
  839. <span class="annotation">@Override</span>
  840. <span class="directive">public</span> Code saveAll(Code item) {
  841. <span class="keyword">assert</span> item.getSource() != <span class="predefined-constant">null</span>;
  842. Code code = <span class="local-variable">this</span>.findById(item.getId());
  843. <span class="keyword">if</span> (code == <span class="predefined-constant">null</span>) {
  844. <span class="local-variable">this</span>.jdbcTemplate.update(psc -&gt; {
  845. <span class="predefined-type">PreparedStatement</span> ps = psc.prepareStatement(SQL_INSERT);
  846. ps.setString(<span class="integer">1</span>, item.getId());
  847. ps.setString(<span class="integer">2</span>, item.getSource());
  848. <span class="keyword">return</span> ps;
  849. });
  850. <span class="keyword">return</span> item;
  851. } <span class="keyword">else</span> {
  852. <span class="local-variable">this</span>.jdbcTemplate.update(psc -&gt; {
  853. <span class="predefined-type">PreparedStatement</span> ps = psc.prepareStatement(SQL_UPDATE);
  854. ps.setString(<span class="integer">1</span>, item.getSource());
  855. ps.setString(<span class="integer">2</span>, item.getId());
  856. <span class="keyword">return</span> ps;
  857. });
  858. <span class="keyword">return</span> code;
  859. }
  860. }
  861. }</code></pre>
  862. </div>
  863. </div>
  864. <div class="listingblock">
  865. <div class="title">io.pivotal.workshop.snippet.repository.SnippetRepository.java</div>
  866. <div class="content">
  867. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository</span>;
  868. <span class="keyword">import</span> <span class="include">java.sql.PreparedStatement</span>;
  869. <span class="keyword">import</span> <span class="include">java.text.SimpleDateFormat</span>;
  870. <span class="keyword">import</span> <span class="include">java.util.Collection</span>;
  871. <span class="keyword">import</span> <span class="include">java.util.Date</span>;
  872. <span class="keyword">import</span> <span class="include">java.util.List</span>;
  873. <span class="keyword">import</span> <span class="include">org.springframework.beans.factory.annotation.Autowired</span>;
  874. <span class="keyword">import</span> <span class="include">org.springframework.dao.EmptyResultDataAccessException</span>;
  875. <span class="keyword">import</span> <span class="include">org.springframework.jdbc.core.JdbcTemplate</span>;
  876. <span class="keyword">import</span> <span class="include">org.springframework.stereotype.Repository</span>;
  877. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.CrossSnippetLanguageCode</span>;
  878. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Snippet</span>;
  879. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.repository.mapper.SnippetRowMapper</span>;
  880. <span class="annotation">@Repository</span>
  881. <span class="directive">public</span> <span class="type">class</span> <span class="class">SnippetRepository</span> <span class="directive">implements</span> SimpleRepository&lt;Snippet&gt; {
  882. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_FIND_ALL = <span class="string"><span class="delimiter">&quot;</span><span class="content">select s.*,l.id lang_id, l.name lang_name, l.syntax lang_syntax, c.id code_id, c.source code_source from cross_snippet_language_code cx </span><span class="delimiter">&quot;</span></span> +
  883. <span class="string"><span class="delimiter">&quot;</span><span class="content">inner join snippet s on cx.snippet_id = s.id </span><span class="delimiter">&quot;</span></span> +
  884. <span class="string"><span class="delimiter">&quot;</span><span class="content">inner join language l on cx.language_id = l.id </span><span class="delimiter">&quot;</span></span> +
  885. <span class="string"><span class="delimiter">&quot;</span><span class="content">inner join code c on cx.code_id = c.id </span><span class="delimiter">&quot;</span></span>;
  886. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_FIND_ONE = SQL_FIND_ALL + <span class="string"><span class="delimiter">&quot;</span><span class="content"> where s.id = ?</span><span class="delimiter">&quot;</span></span>;
  887. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_INSERT = <span class="string"><span class="delimiter">&quot;</span><span class="content">insert into snippet(id,title, keywords, description, created, modified) values(?,?,?,?,?,?)</span><span class="delimiter">&quot;</span></span>;
  888. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_UPDATE = <span class="string"><span class="delimiter">&quot;</span><span class="content">update snippet set title = ?, keywords = ?, description = ?, modified = ? where id = ?</span><span class="delimiter">&quot;</span></span>;
  889. <span class="directive">private</span> JdbcTemplate jdbcTemplate;
  890. <span class="directive">private</span> LanguageRepository langRepo;
  891. <span class="directive">private</span> CodeRepository codeRepo;
  892. <span class="directive">private</span> CrossSnippetLanguageCodeRepository crossRepo;
  893. <span class="annotation">@Autowired</span>
  894. <span class="directive">public</span> SnippetRepository(JdbcTemplate jdbcTemplate, LanguageRepository langRepo, CodeRepository codeRepo, CrossSnippetLanguageCodeRepository crossRepo) {
  895. <span class="local-variable">this</span>.jdbcTemplate = jdbcTemplate;
  896. <span class="local-variable">this</span>.langRepo = langRepo;
  897. <span class="local-variable">this</span>.codeRepo = codeRepo;
  898. <span class="local-variable">this</span>.crossRepo = crossRepo;
  899. }
  900. <span class="annotation">@Override</span>
  901. <span class="directive">public</span> <span class="predefined-type">List</span>&lt;Snippet&gt; findAll() {
  902. <span class="keyword">return</span> <span class="local-variable">this</span>.jdbcTemplate.query(SQL_FIND_ALL, <span class="keyword">new</span> SnippetRowMapper());
  903. }
  904. <span class="annotation">@Override</span>
  905. <span class="directive">public</span> <span class="type">void</span> saveAll(<span class="predefined-type">Collection</span>&lt;Snippet&gt; items) {
  906. items.forEach(code -&gt; <span class="local-variable">this</span>.saveAll(code));
  907. }
  908. <span class="annotation">@Override</span>
  909. <span class="directive">public</span> Snippet findById(<span class="predefined-type">String</span> id) {
  910. <span class="keyword">try</span> {
  911. <span class="keyword">return</span> <span class="local-variable">this</span>.jdbcTemplate.queryForObject(SQL_FIND_ONE, <span class="keyword">new</span> <span class="predefined-type">Object</span><span class="type">[]</span> { id }, <span class="keyword">new</span> SnippetRowMapper());
  912. } <span class="keyword">catch</span> (EmptyResultDataAccessException ex) {
  913. <span class="keyword">return</span> <span class="predefined-constant">null</span>;
  914. }
  915. }
  916. <span class="annotation">@Override</span>
  917. <span class="directive">public</span> Snippet saveAll(<span class="directive">final</span> Snippet item) {
  918. <span class="keyword">assert</span> item.getId() != <span class="predefined-constant">null</span>;
  919. <span class="keyword">assert</span> item.getTitle() != <span class="predefined-constant">null</span>;
  920. <span class="keyword">assert</span> item.getLang() != <span class="predefined-constant">null</span>;
  921. <span class="keyword">assert</span> item.getCode() != <span class="predefined-constant">null</span>;
  922. Snippet snippet = <span class="local-variable">this</span>.findById(item.getId());
  923. <span class="predefined-type">Date</span> date = <span class="keyword">new</span> <span class="predefined-type">Date</span>();
  924. <span class="predefined-type">SimpleDateFormat</span> dateFormat = <span class="keyword">new</span> <span class="predefined-type">SimpleDateFormat</span>(<span class="string"><span class="delimiter">&quot;</span><span class="content">yyyy-MM-dd</span><span class="delimiter">&quot;</span></span>);
  925. <span class="keyword">if</span> (snippet == <span class="predefined-constant">null</span>) {
  926. <span class="local-variable">this</span>.crossRepo.saveAll(<span class="keyword">new</span> CrossSnippetLanguageCode(item.getId(), item.getLang().getId(), item.getCode().getId()));
  927. <span class="local-variable">this</span>.langRepo.saveAll(item.getLang());
  928. <span class="local-variable">this</span>.codeRepo.saveAll(item.getCode());
  929. <span class="local-variable">this</span>.jdbcTemplate.update(psc -&gt; {
  930. <span class="predefined-type">PreparedStatement</span> ps = psc.prepareStatement(SQL_INSERT);
  931. ps.setString(<span class="integer">1</span>, item.getId());
  932. ps.setString(<span class="integer">2</span>, item.getTitle());
  933. ps.setString(<span class="integer">3</span>, item.getKeywords());
  934. ps.setString(<span class="integer">4</span>, item.getDescription());
  935. ps.setString(<span class="integer">5</span>, dateFormat.format(date));
  936. ps.setString(<span class="integer">6</span>, dateFormat.format(date));
  937. <span class="keyword">return</span> ps;
  938. });
  939. <span class="keyword">return</span> item;
  940. } <span class="keyword">else</span> {
  941. <span class="local-variable">this</span>.crossRepo.saveAll(<span class="keyword">new</span> CrossSnippetLanguageCode(snippet.getId(), snippet.getLang().getId(), snippet.getCode().getId()));
  942. <span class="local-variable">this</span>.langRepo.saveAll(item.getLang());
  943. <span class="local-variable">this</span>.codeRepo.saveAll(item.getCode());
  944. <span class="local-variable">this</span>.jdbcTemplate.update(psc -&gt; {
  945. <span class="predefined-type">PreparedStatement</span> ps = psc.prepareStatement(SQL_UPDATE);
  946. ps.setString(<span class="integer">1</span>, item.getTitle());
  947. ps.setString(<span class="integer">2</span>, item.getKeywords());
  948. ps.setString(<span class="integer">3</span>, item.getDescription());
  949. ps.setString(<span class="integer">4</span>, dateFormat.format(date));
  950. ps.setString(<span class="integer">5</span>, item.getId());
  951. <span class="keyword">return</span> ps;
  952. });
  953. <span class="keyword">return</span> snippet;
  954. }
  955. }
  956. }</code></pre>
  957. </div>
  958. </div>
  959. <div class="paragraph">
  960. <p>Every single class is using the <strong><em>JdbcTemplate</em></strong> (with some methods like: <strong>queryForObject</strong>, <strong>query</strong> and <strong>update</strong>) and the <strong><em>RowMapper</em></strong>, analyze the code and review the <strong>SQL</strong> statements.</p>
  961. </div>
  962. </li>
  963. <li>
  964. <p>As you already guess from the code above, the <strong>SnippetRepository</strong> class has on its constructor the <strong>CrossSnippetLanguageCodeRepository</strong> reference. Create this class:</p>
  965. <div class="listingblock">
  966. <div class="title">io.pivotal.workshop.snippet.repository.CrossSnippetLanguageCodeRepository.java</div>
  967. <div class="content">
  968. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository</span>;
  969. <span class="keyword">import</span> <span class="include">java.sql.PreparedStatement</span>;
  970. <span class="keyword">import</span> <span class="include">java.sql.ResultSet</span>;
  971. <span class="keyword">import</span> <span class="include">java.util.Collection</span>;
  972. <span class="keyword">import</span> <span class="include">org.springframework.beans.factory.annotation.Autowired</span>;
  973. <span class="keyword">import</span> <span class="include">org.springframework.dao.EmptyResultDataAccessException</span>;
  974. <span class="keyword">import</span> <span class="include">org.springframework.jdbc.core.JdbcTemplate</span>;
  975. <span class="keyword">import</span> <span class="include">org.springframework.jdbc.core.RowMapper</span>;
  976. <span class="keyword">import</span> <span class="include">org.springframework.stereotype.Repository</span>;
  977. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.CrossSnippetLanguageCode</span>;
  978. <span class="annotation">@Repository</span>
  979. <span class="directive">public</span> <span class="type">class</span> <span class="class">CrossSnippetLanguageCodeRepository</span> <span class="directive">implements</span> SimpleRepository&lt;CrossSnippetLanguageCode&gt; {
  980. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_FIND_ALL = <span class="string"><span class="delimiter">&quot;</span><span class="content">select * from cross_snippet_language_code</span><span class="delimiter">&quot;</span></span>;
  981. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_FIND_ONE = <span class="string"><span class="delimiter">&quot;</span><span class="content">select * from cross_snippet_language_code where snippet_id = ?</span><span class="delimiter">&quot;</span></span>;
  982. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_INSERT = <span class="string"><span class="delimiter">&quot;</span><span class="content">insert into cross_snippet_language_code(snippet_id,language_id,code_id) values(?,?,?)</span><span class="delimiter">&quot;</span></span>;
  983. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">String</span> SQL_UPDATE = <span class="string"><span class="delimiter">&quot;</span><span class="content">update cross_snippet_language_code set langauge_id = ?, code_id = ? where snippet_id = ?</span><span class="delimiter">&quot;</span></span>;
  984. <span class="directive">private</span> JdbcTemplate jdbcTemplate;
  985. <span class="directive">private</span> <span class="directive">final</span> <span class="predefined-type">RowMapper</span>&lt;CrossSnippetLanguageCode&gt; rowMapper = (<span class="predefined-type">ResultSet</span> rs, <span class="type">int</span> row) -&gt; {
  986. CrossSnippetLanguageCode cross = <span class="keyword">new</span> CrossSnippetLanguageCode();
  987. cross.setSnippetId(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">snippet_id</span><span class="delimiter">&quot;</span></span>));
  988. cross.setLanguageId(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">language_id</span><span class="delimiter">&quot;</span></span>));
  989. cross.setCodeId(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">code_id</span><span class="delimiter">&quot;</span></span>));
  990. <span class="keyword">return</span> cross;
  991. };
  992. <span class="annotation">@Autowired</span>
  993. <span class="directive">public</span> CrossSnippetLanguageCodeRepository(JdbcTemplate jdbcTemplate) {
  994. <span class="local-variable">this</span>.jdbcTemplate = jdbcTemplate;
  995. }
  996. <span class="annotation">@Override</span>
  997. <span class="directive">public</span> <span class="predefined-type">Iterable</span>&lt;CrossSnippetLanguageCode&gt; findAll() {
  998. <span class="keyword">return</span> <span class="local-variable">this</span>.jdbcTemplate.query(SQL_FIND_ALL, rowMapper);
  999. }
  1000. <span class="annotation">@Override</span>
  1001. <span class="directive">public</span> <span class="type">void</span> saveAll(<span class="predefined-type">Collection</span>&lt;CrossSnippetLanguageCode&gt; items) {
  1002. items.forEach(cross -&gt; {
  1003. <span class="local-variable">this</span>.saveAll(cross);
  1004. });
  1005. }
  1006. <span class="annotation">@Override</span>
  1007. <span class="directive">public</span> CrossSnippetLanguageCode saveAll(CrossSnippetLanguageCode item) {
  1008. <span class="keyword">assert</span> item.getSnippetId() != <span class="predefined-constant">null</span>;
  1009. <span class="keyword">assert</span> item.getLanguageId() != <span class="predefined-constant">null</span>;
  1010. <span class="keyword">assert</span> item.getCodeId() != <span class="predefined-constant">null</span>;
  1011. CrossSnippetLanguageCode cross = <span class="local-variable">this</span>.findById(item.getSnippetId());
  1012. <span class="keyword">if</span> (cross == <span class="predefined-constant">null</span>) {
  1013. <span class="local-variable">this</span>.jdbcTemplate.update(psc -&gt; {
  1014. <span class="predefined-type">PreparedStatement</span> ps = psc.prepareStatement(SQL_INSERT);
  1015. ps.setString(<span class="integer">1</span>, item.getSnippetId());
  1016. ps.setString(<span class="integer">2</span>, item.getLanguageId());
  1017. ps.setString(<span class="integer">3</span>, item.getCodeId());
  1018. <span class="keyword">return</span> ps;
  1019. });
  1020. <span class="keyword">return</span> item;
  1021. } <span class="keyword">else</span> {
  1022. <span class="local-variable">this</span>.jdbcTemplate.update(psc -&gt; {
  1023. <span class="predefined-type">PreparedStatement</span> ps = psc.prepareStatement(SQL_UPDATE);
  1024. ps.setString(<span class="integer">1</span>, item.getLanguageId());
  1025. ps.setString(<span class="integer">2</span>, item.getCodeId());
  1026. ps.setString(<span class="integer">3</span>, item.getSnippetId());
  1027. <span class="keyword">return</span> ps;
  1028. });
  1029. <span class="keyword">return</span> cross;
  1030. }
  1031. }
  1032. <span class="annotation">@Override</span>
  1033. <span class="directive">public</span> CrossSnippetLanguageCode findById(<span class="predefined-type">String</span> id) {
  1034. <span class="keyword">try</span> {
  1035. <span class="keyword">return</span> <span class="local-variable">this</span>.jdbcTemplate.queryForObject(SQL_FIND_ONE, <span class="keyword">new</span> <span class="predefined-type">Object</span><span class="type">[]</span> { id }, rowMapper);
  1036. } <span class="keyword">catch</span> (EmptyResultDataAccessException ex) {
  1037. <span class="keyword">return</span> <span class="predefined-constant">null</span>;
  1038. }
  1039. }
  1040. }</code></pre>
  1041. </div>
  1042. </div>
  1043. <div class="paragraph">
  1044. <p>Have you noticed that the <strong>CrossSnippetLanguageCodeRepository</strong> is using a declared <strong>Java 8 lambda</strong> notation for the <strong>RowMapper</strong>?</p>
  1045. </div>
  1046. </li>
  1047. <li>
  1048. <p>Talking about <strong>RowMapper</strong>, all the above code is using it to map the <strong>ResultSet</strong> with the domain class. Create the following mappers in the <strong>io.pivotal.workshop.snippet.repository.mapper</strong> package:</p>
  1049. <div class="listingblock">
  1050. <div class="title">io.pivotal.workshop.snippet.repository.mapper.LanguageRowMapper.java</div>
  1051. <div class="content">
  1052. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository.mapper</span>;
  1053. <span class="keyword">import</span> <span class="include">java.sql.ResultSet</span>;
  1054. <span class="keyword">import</span> <span class="include">java.sql.SQLException</span>;
  1055. <span class="keyword">import</span> <span class="include">org.springframework.jdbc.core.RowMapper</span>;
  1056. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Language</span>;
  1057. <span class="directive">public</span> <span class="type">class</span> <span class="class">LanguageRowMapper</span> <span class="directive">implements</span> <span class="predefined-type">RowMapper</span>&lt;Language&gt; {
  1058. <span class="annotation">@Override</span>
  1059. <span class="directive">public</span> Language mapRow(<span class="predefined-type">ResultSet</span> rs, <span class="type">int</span> rowNum) <span class="directive">throws</span> <span class="exception">SQLException</span> {
  1060. Language lang = <span class="keyword">new</span> Language();
  1061. lang.setId(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">id</span><span class="delimiter">&quot;</span></span>));
  1062. lang.setName(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">name</span><span class="delimiter">&quot;</span></span>));
  1063. lang.setSyntax(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">syntax</span><span class="delimiter">&quot;</span></span>));
  1064. <span class="keyword">return</span> lang;
  1065. }
  1066. }</code></pre>
  1067. </div>
  1068. </div>
  1069. <div class="listingblock">
  1070. <div class="title">io.pivotal.workshop.snippet.repository.mapper.CodeRowMapper.java</div>
  1071. <div class="content">
  1072. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository.mapper</span>;
  1073. <span class="keyword">import</span> <span class="include">java.sql.ResultSet</span>;
  1074. <span class="keyword">import</span> <span class="include">java.sql.SQLException</span>;
  1075. <span class="keyword">import</span> <span class="include">org.springframework.jdbc.core.RowMapper</span>;
  1076. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Code</span>;
  1077. <span class="directive">public</span> <span class="type">class</span> <span class="class">CodeRowMapper</span> <span class="directive">implements</span> <span class="predefined-type">RowMapper</span>&lt;Code&gt; {
  1078. <span class="annotation">@Override</span>
  1079. <span class="directive">public</span> Code mapRow(<span class="predefined-type">ResultSet</span> rs, <span class="type">int</span> rowNum) <span class="directive">throws</span> <span class="exception">SQLException</span> {
  1080. Code code = <span class="keyword">new</span> Code();
  1081. code.setId(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">id</span><span class="delimiter">&quot;</span></span>));
  1082. code.setSource(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">source</span><span class="delimiter">&quot;</span></span>));
  1083. <span class="keyword">return</span> code;
  1084. }
  1085. }</code></pre>
  1086. </div>
  1087. </div>
  1088. <div class="listingblock">
  1089. <div class="title">io.pivotal.workshop.snippet.repository.mapper.SnippetRowMapper.java</div>
  1090. <div class="content">
  1091. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository.mapper</span>;
  1092. <span class="keyword">import</span> <span class="include">java.sql.ResultSet</span>;
  1093. <span class="keyword">import</span> <span class="include">java.sql.SQLException</span>;
  1094. <span class="keyword">import</span> <span class="include">org.springframework.jdbc.core.RowMapper</span>;
  1095. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Code</span>;
  1096. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Language</span>;
  1097. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Snippet</span>;
  1098. <span class="directive">public</span> <span class="type">class</span> <span class="class">SnippetRowMapper</span> <span class="directive">implements</span> <span class="predefined-type">RowMapper</span>&lt;Snippet&gt; {
  1099. <span class="annotation">@Override</span>
  1100. <span class="directive">public</span> Snippet mapRow(<span class="predefined-type">ResultSet</span> rs, <span class="type">int</span> rowNum) <span class="directive">throws</span> <span class="exception">SQLException</span> {
  1101. Language lang = <span class="keyword">new</span> Language();
  1102. lang.setId(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">lang_id</span><span class="delimiter">&quot;</span></span>));
  1103. lang.setName(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">lang_name</span><span class="delimiter">&quot;</span></span>));
  1104. lang.setSyntax(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">lang_syntax</span><span class="delimiter">&quot;</span></span>));
  1105. Code code = <span class="keyword">new</span> Code();
  1106. code.setId(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">code_id</span><span class="delimiter">&quot;</span></span>));
  1107. code.setSource(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">code_source</span><span class="delimiter">&quot;</span></span>));
  1108. Snippet snippet = <span class="keyword">new</span> Snippet();
  1109. snippet.setId(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">id</span><span class="delimiter">&quot;</span></span>));
  1110. snippet.setTitle(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">title</span><span class="delimiter">&quot;</span></span>));
  1111. snippet.setDescription(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">description</span><span class="delimiter">&quot;</span></span>));
  1112. snippet.setKeywords(rs.getString(<span class="string"><span class="delimiter">&quot;</span><span class="content">keywords</span><span class="delimiter">&quot;</span></span>));
  1113. snippet.setCreated(rs.getDate(<span class="string"><span class="delimiter">&quot;</span><span class="content">created</span><span class="delimiter">&quot;</span></span>));
  1114. snippet.setModified(rs.getDate(<span class="string"><span class="delimiter">&quot;</span><span class="content">modified</span><span class="delimiter">&quot;</span></span>));
  1115. snippet.setLang(lang);
  1116. snippet.setCode(code);
  1117. <span class="keyword">return</span> snippet;
  1118. }
  1119. }</code></pre>
  1120. </div>
  1121. </div>
  1122. </li>
  1123. <li>
  1124. <p>The <strong>SnippetController</strong> and the <strong>SnippetConfiguration</strong> classes must be the same. Practically there is no change on any of them, they should work.</p>
  1125. </li>
  1126. <li>
  1127. <p>Next add the <strong>schema.sql</strong> in the <strong>src/main/resources/</strong> folder.</p>
  1128. <div class="listingblock">
  1129. <div class="content">
  1130. <pre class="CodeRay highlight"><code data-lang="sql"><span class="class">DROP</span> <span class="type">TABLE</span> <span class="keyword">IF</span> <span class="keyword">EXISTS</span> snippet;
  1131. <span class="class">CREATE</span> <span class="type">TABLE</span> snippet
  1132. (
  1133. id <span class="predefined-type">varchar</span>(<span class="integer">36</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1134. title <span class="predefined-type">varchar</span>(<span class="integer">200</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1135. keywords <span class="predefined-type">varchar</span>(<span class="integer">250</span>) <span class="directive">DEFAULT</span> <span class="predefined-constant">NULL</span>,
  1136. description <span class="predefined-type">varchar</span>(<span class="integer">500</span>) <span class="directive">DEFAULT</span> <span class="predefined-constant">NULL</span>,
  1137. created <span class="predefined-type">date</span> <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1138. modified <span class="predefined-type">date</span> <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1139. <span class="directive">PRIMARY</span> <span class="type">KEY</span> (id)
  1140. );
  1141. <span class="class">DROP</span> <span class="type">TABLE</span> <span class="keyword">IF</span> <span class="keyword">EXISTS</span> language;
  1142. <span class="class">CREATE</span> <span class="type">TABLE</span> language
  1143. (
  1144. id <span class="predefined-type">varchar</span>(<span class="integer">36</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1145. name <span class="predefined-type">varchar</span>(<span class="integer">250</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1146. syntax <span class="predefined-type">varchar</span>(<span class="integer">250</span>) <span class="directive">DEFAULT</span> <span class="predefined-constant">NULL</span>,
  1147. <span class="directive">PRIMARY</span> <span class="type">KEY</span> (id)
  1148. );
  1149. <span class="class">DROP</span> <span class="type">TABLE</span> <span class="keyword">IF</span> <span class="keyword">EXISTS</span> code;
  1150. <span class="class">CREATE</span> <span class="type">TABLE</span> code
  1151. (
  1152. id <span class="predefined-type">varchar</span>(<span class="integer">36</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1153. source <span class="predefined-type">varchar</span>(<span class="integer">5000</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1154. <span class="directive">PRIMARY</span> <span class="type">KEY</span> (id)
  1155. );
  1156. <span class="class">DROP</span> <span class="type">TABLE</span> <span class="keyword">IF</span> <span class="keyword">EXISTS</span> cross_snippet_language_code;
  1157. <span class="class">CREATE</span> <span class="type">TABLE</span> cross_snippet_language_code
  1158. (
  1159. snippet_id <span class="predefined-type">varchar</span>(<span class="integer">36</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1160. language_id <span class="predefined-type">varchar</span>(<span class="integer">36</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1161. code_id <span class="predefined-type">varchar</span>(<span class="integer">36</span>) <span class="keyword">NOT</span> <span class="predefined-constant">NULL</span>,
  1162. <span class="directive">PRIMARY</span> <span class="type">KEY</span> (snippet_id)
  1163. );</code></pre>
  1164. </div>
  1165. </div>
  1166. </li>
  1167. </ol>
  1168. </div>
  1169. <div class="admonitionblock tip">
  1170. <table>
  1171. <tr>
  1172. <td class="icon">
  1173. <div class="title">Tip</div>
  1174. </td>
  1175. <td class="content">
  1176. Before you run your application make sure to have copied <strong>resources/static</strong> and <strong>resources/templates</strong> files from the previous labs.
  1177. </td>
  1178. </tr>
  1179. </table>
  1180. </div>
  1181. <div class="sect2">
  1182. <h3 id="_challenges">Challenges</h3>
  1183. <div class="ulist">
  1184. <ul>
  1185. <li>
  1186. <p>Run the application and make sure it works.</p>
  1187. </li>
  1188. <li>
  1189. <p>Do any necessary change to have the home page working.</p>
  1190. <div class="paragraph">
  1191. <div class="title">Figure 1.1: Code Snippet Manager JDBC - <a href="http://localhost:8080/" class="bare" target="_blank">http://localhost:8080/</a></div>
  1192. <p><span class="image"><img src="images/06-spring-boot-data-04.png" alt="Code Snippet Manager" title="Spring Initializr"></span></p>
  1193. </div>
  1194. </li>
  1195. </ul>
  1196. </div>
  1197. </div>
  1198. </div>
  1199. </div>
  1200. <div class="sect1">
  1201. <h2 id="_code_snippet_manager_jpa">Code Snippet Manager JPA</h2>
  1202. <div class="sectionbody">
  1203. <div class="paragraph">
  1204. <p>You will continue with the <strong>Code Snippet Manager</strong> code, but this time using <strong>JPA</strong>. You will reuse the code from previous labs.</p>
  1205. </div>
  1206. <div class="olist arabic">
  1207. <ol class="arabic">
  1208. <li>
  1209. <p>Open a browser and hit the url: <a href="http://start.spring.io" class="bare" target="_blank">http://start.spring.io</a></p>
  1210. </li>
  1211. <li>
  1212. <p>Click the <strong><em>Switch to the full version</em></strong> link.</p>
  1213. </li>
  1214. <li>
  1215. <p>Fill out the <strong>Code Snippet Manager JPA</strong> Project metadata with (See Figure 2.0):</p>
  1216. <table class="tableblock frame-all grid-all spread">
  1217. <caption class="title">Table 2. Code Snippet Manager JPA App - metadata</caption>
  1218. <colgroup>
  1219. <col style="width: 16.6666%;">
  1220. <col style="width: 83.3334%;">
  1221. </colgroup>
  1222. <thead>
  1223. <tr>
  1224. <th class="tableblock halign-left valign-top">Property</th>
  1225. <th class="tableblock halign-left valign-top">Value</th>
  1226. </tr>
  1227. </thead>
  1228. <tbody>
  1229. <tr>
  1230. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Group</strong>:</p></td>
  1231. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>io.pivotal.workshop</em></p></td>
  1232. </tr>
  1233. <tr>
  1234. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Artifact</strong>:</p></td>
  1235. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>code-snippet-manager-jpa</em></p></td>
  1236. </tr>
  1237. <tr>
  1238. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Name</strong>:</p></td>
  1239. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>code-snippet-manager-jpa</em></p></td>
  1240. </tr>
  1241. <tr>
  1242. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Package Name</strong>:</p></td>
  1243. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>io.pivotal.workshop.snippet</em></p></td>
  1244. </tr>
  1245. <tr>
  1246. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Dependencies</strong>:</p></td>
  1247. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>Web, DevTools, Groovy Templates, JPA, H2, MySQL</em></p></td>
  1248. </tr>
  1249. <tr>
  1250. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Spring Boot</strong>:</p></td>
  1251. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>2.0.0.M7</em></p></td>
  1252. </tr>
  1253. </tbody>
  1254. </table>
  1255. <div class="paragraph">
  1256. <div class="title">Figure 2.0: Spring Initializr - <a href="http://start.spring.io/" class="bare" target="_blank">http://start.spring.io/</a></div>
  1257. <p><span class="image"><img src="images/06-spring-boot-data-02.png" alt="SpringInitializr" title="Spring Initializr"></span></p>
  1258. </div>
  1259. <div class="admonitionblock tip">
  1260. <table>
  1261. <tr>
  1262. <td class="icon">
  1263. <div class="title">Tip</div>
  1264. </td>
  1265. <td class="content">
  1266. You can choose either <strong>Maven</strong> or <strong>Gradle</strong> project types.
  1267. </td>
  1268. </tr>
  1269. </table>
  1270. </div>
  1271. </li>
  1272. <li>
  1273. <p>type <strong>Web</strong>, <strong>DevTools</strong>, <strong>Groovy Templates</strong>, <strong>JPA</strong>, <strong>H2</strong>, and <strong>MySQL</strong> in the <strong><em>Dependencies</em></strong> field and press Enter.</p>
  1274. </li>
  1275. <li>
  1276. <p>Click the <strong><em>Generate Project</em></strong> button.</p>
  1277. </li>
  1278. <li>
  1279. <p>Unzip the file in any directory you want.</p>
  1280. </li>
  1281. <li>
  1282. <p>Import your project in any IDE you want.</p>
  1283. </li>
  1284. <li>
  1285. <p>Copy all the packages (with code) into the new project.</p>
  1286. </li>
  1287. <li>
  1288. <p>The classes in <strong>io.pivotal.workshop.snippet.domain</strong>: <strong><em>Code</em></strong>, <strong><em>Language</em></strong> and <strong><em>Snippet</em></strong> will change because you will use the <strong>JPA</strong> features. Create/modify the following classes:</p>
  1289. <div class="listingblock">
  1290. <div class="title">io.pivotal.workshop.snippet.domain.Language.java</div>
  1291. <div class="content">
  1292. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.domain</span>;
  1293. <span class="keyword">import</span> <span class="include">javax.persistence.Entity</span>;
  1294. <span class="keyword">import</span> <span class="include">javax.persistence.GeneratedValue</span>;
  1295. <span class="keyword">import</span> <span class="include">javax.persistence.Id</span>;
  1296. <span class="keyword">import</span> <span class="include">org.hibernate.annotations.GenericGenerator</span>;
  1297. <span class="annotation">@Entity</span>
  1298. <span class="directive">public</span> <span class="type">class</span> <span class="class">Language</span> {
  1299. <span class="annotation">@Id</span>
  1300. <span class="annotation">@GeneratedValue</span>(generator = <span class="string"><span class="delimiter">&quot;</span><span class="content">system-uuid</span><span class="delimiter">&quot;</span></span>)
  1301. <span class="annotation">@GenericGenerator</span>(name = <span class="string"><span class="delimiter">&quot;</span><span class="content">system-uuid</span><span class="delimiter">&quot;</span></span>, strategy = <span class="string"><span class="delimiter">&quot;</span><span class="content">uuid</span><span class="delimiter">&quot;</span></span>)
  1302. <span class="directive">private</span> <span class="predefined-type">String</span> id;
  1303. <span class="directive">private</span> <span class="predefined-type">String</span> name;
  1304. <span class="directive">private</span> <span class="predefined-type">String</span> syntax = <span class="string"><span class="delimiter">&quot;</span><span class="content">text</span><span class="delimiter">&quot;</span></span>;
  1305. <span class="directive">public</span> Language() {
  1306. }
  1307. <span class="directive">public</span> Language(<span class="predefined-type">String</span> name) {
  1308. <span class="local-variable">this</span>();
  1309. <span class="local-variable">this</span>.name = name;
  1310. }
  1311. <span class="directive">public</span> Language(<span class="predefined-type">String</span> name, <span class="predefined-type">String</span> syntax) {
  1312. <span class="local-variable">this</span>(name);
  1313. <span class="local-variable">this</span>.syntax = syntax;
  1314. }
  1315. <span class="directive">public</span> <span class="predefined-type">String</span> getId() {
  1316. <span class="keyword">return</span> id;
  1317. }
  1318. <span class="directive">public</span> <span class="type">void</span> setId(<span class="predefined-type">String</span> id) {
  1319. <span class="local-variable">this</span>.id = id;
  1320. }
  1321. <span class="directive">public</span> <span class="predefined-type">String</span> getName() {
  1322. <span class="keyword">return</span> name;
  1323. }
  1324. <span class="directive">public</span> <span class="type">void</span> setName(<span class="predefined-type">String</span> name) {
  1325. <span class="local-variable">this</span>.name = name;
  1326. }
  1327. <span class="directive">public</span> <span class="predefined-type">String</span> getSyntax() {
  1328. <span class="keyword">return</span> syntax;
  1329. }
  1330. <span class="directive">public</span> <span class="type">void</span> setSyntax(<span class="predefined-type">String</span> syntax) {
  1331. <span class="local-variable">this</span>.syntax = syntax;
  1332. }
  1333. }</code></pre>
  1334. </div>
  1335. </div>
  1336. <div class="listingblock">
  1337. <div class="title">io.pivotal.workshop.snippet.domain.Code.java</div>
  1338. <div class="content">
  1339. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.domain</span>;
  1340. <span class="keyword">import</span> <span class="include">javax.persistence.Entity</span>;
  1341. <span class="keyword">import</span> <span class="include">javax.persistence.GeneratedValue</span>;
  1342. <span class="keyword">import</span> <span class="include">javax.persistence.Id</span>;
  1343. <span class="keyword">import</span> <span class="include">org.hibernate.annotations.GenericGenerator</span>;
  1344. <span class="annotation">@Entity</span>
  1345. <span class="directive">public</span> <span class="type">class</span> <span class="class">Code</span> {
  1346. <span class="annotation">@Id</span>
  1347. <span class="annotation">@GeneratedValue</span>(generator = <span class="string"><span class="delimiter">&quot;</span><span class="content">system-uuid</span><span class="delimiter">&quot;</span></span>)
  1348. <span class="annotation">@GenericGenerator</span>(name = <span class="string"><span class="delimiter">&quot;</span><span class="content">system-uuid</span><span class="delimiter">&quot;</span></span>, strategy = <span class="string"><span class="delimiter">&quot;</span><span class="content">uuid</span><span class="delimiter">&quot;</span></span>)
  1349. <span class="directive">private</span> <span class="predefined-type">String</span> id;
  1350. <span class="directive">private</span> <span class="predefined-type">String</span> source;
  1351. <span class="directive">public</span> Code() {
  1352. }
  1353. <span class="directive">public</span> Code(<span class="predefined-type">String</span> source) {
  1354. <span class="local-variable">this</span>();
  1355. <span class="local-variable">this</span>.source = source;
  1356. }
  1357. <span class="directive">public</span> <span class="predefined-type">String</span> getId() {
  1358. <span class="keyword">return</span> id;
  1359. }
  1360. <span class="directive">public</span> <span class="type">void</span> setId(<span class="predefined-type">String</span> id) {
  1361. <span class="local-variable">this</span>.id = id;
  1362. }
  1363. <span class="directive">public</span> <span class="predefined-type">String</span> getSource() {
  1364. <span class="keyword">return</span> source;
  1365. }
  1366. <span class="directive">public</span> <span class="type">void</span> setSource(<span class="predefined-type">String</span> source) {
  1367. <span class="local-variable">this</span>.source = source;
  1368. }
  1369. }</code></pre>
  1370. </div>
  1371. </div>
  1372. <div class="listingblock">
  1373. <div class="title">io.pivotal.workshop.snippet.domain.Snippet.java</div>
  1374. <div class="content">
  1375. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.domain</span>;
  1376. <span class="keyword">import</span> <span class="include">java.util.Date</span>;
  1377. <span class="keyword">import</span> <span class="include">javax.persistence.CascadeType</span>;
  1378. <span class="keyword">import</span> <span class="include">javax.persistence.Column</span>;
  1379. <span class="keyword">import</span> <span class="include">javax.persistence.Entity</span>;
  1380. <span class="keyword">import</span> <span class="include">javax.persistence.FetchType</span>;
  1381. <span class="keyword">import</span> <span class="include">javax.persistence.GeneratedValue</span>;
  1382. <span class="keyword">import</span> <span class="include">javax.persistence.Id</span>;
  1383. <span class="keyword">import</span> <span class="include">javax.persistence.OneToOne</span>;
  1384. <span class="keyword">import</span> <span class="include">javax.persistence.PrePersist</span>;
  1385. <span class="keyword">import</span> <span class="include">javax.persistence.PreUpdate</span>;
  1386. <span class="keyword">import</span> <span class="include">org.hibernate.annotations.GenericGenerator</span>;
  1387. <span class="annotation">@Entity</span>
  1388. <span class="directive">public</span> <span class="type">class</span> <span class="class">Snippet</span> {
  1389. <span class="annotation">@Id</span>
  1390. <span class="annotation">@GeneratedValue</span>(generator = <span class="string"><span class="delimiter">&quot;</span><span class="content">system-uuid</span><span class="delimiter">&quot;</span></span>)
  1391. <span class="annotation">@GenericGenerator</span>(name = <span class="string"><span class="delimiter">&quot;</span><span class="content">system-uuid</span><span class="delimiter">&quot;</span></span>, strategy = <span class="string"><span class="delimiter">&quot;</span><span class="content">uuid</span><span class="delimiter">&quot;</span></span>)
  1392. <span class="directive">private</span> <span class="predefined-type">String</span> id;
  1393. <span class="directive">private</span> <span class="predefined-type">String</span> title;
  1394. <span class="directive">private</span> <span class="predefined-type">String</span> keywords = <span class="string"><span class="delimiter">&quot;</span><span class="delimiter">&quot;</span></span>;
  1395. <span class="directive">private</span> <span class="predefined-type">String</span> description = <span class="string"><span class="delimiter">&quot;</span><span class="delimiter">&quot;</span></span>;
  1396. <span class="annotation">@OneToOne</span>(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
  1397. <span class="directive">private</span> Language lang;
  1398. <span class="annotation">@OneToOne</span>(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
  1399. <span class="directive">private</span> Code code;
  1400. <span class="annotation">@Column</span>(insertable = <span class="predefined-constant">true</span>, updatable = <span class="predefined-constant">false</span>)
  1401. <span class="directive">private</span> <span class="predefined-type">Date</span> created;
  1402. <span class="directive">private</span> <span class="predefined-type">Date</span> modified;
  1403. <span class="directive">public</span> Snippet() {
  1404. <span class="local-variable">this</span>.id = java.util.UUID.randomUUID().toString().replaceAll(<span class="string"><span class="delimiter">&quot;</span><span class="content">-</span><span class="delimiter">&quot;</span></span>, <span class="string"><span class="delimiter">&quot;</span><span class="delimiter">&quot;</span></span>);
  1405. <span class="local-variable">this</span>.created = <span class="keyword">new</span> <span class="predefined-type">Date</span>();
  1406. <span class="local-variable">this</span>.modified = <span class="keyword">new</span> <span class="predefined-type">Date</span>();
  1407. }
  1408. <span class="directive">public</span> Snippet(<span class="predefined-type">String</span> title, <span class="predefined-type">String</span> keywords, <span class="predefined-type">String</span> description, Language lang, Code code) {
  1409. <span class="local-variable">this</span>();
  1410. <span class="local-variable">this</span>.title = title;
  1411. <span class="local-variable">this</span>.keywords = keywords;
  1412. <span class="local-variable">this</span>.description = description;
  1413. <span class="local-variable">this</span>.lang = lang;
  1414. <span class="local-variable">this</span>.code = code;
  1415. }
  1416. <span class="directive">public</span> Snippet(<span class="predefined-type">String</span> title, Language lang, Code code) {
  1417. <span class="local-variable">this</span>(title, <span class="string"><span class="delimiter">&quot;</span><span class="delimiter">&quot;</span></span>, <span class="string"><span class="delimiter">&quot;</span><span class="delimiter">&quot;</span></span>, lang, code);
  1418. }
  1419. <span class="directive">public</span> <span class="predefined-type">String</span> getId() {
  1420. <span class="keyword">return</span> id;
  1421. }
  1422. <span class="directive">public</span> <span class="type">void</span> setId(<span class="predefined-type">String</span> id) {
  1423. <span class="local-variable">this</span>.id = id;
  1424. }
  1425. <span class="directive">public</span> <span class="predefined-type">String</span> getTitle() {
  1426. <span class="keyword">return</span> title;
  1427. }
  1428. <span class="directive">public</span> <span class="type">void</span> setTitle(<span class="predefined-type">String</span> title) {
  1429. <span class="local-variable">this</span>.title = title;
  1430. }
  1431. <span class="directive">public</span> Language getLang() {
  1432. <span class="keyword">return</span> lang;
  1433. }
  1434. <span class="directive">public</span> <span class="type">void</span> setLang(Language lang) {
  1435. <span class="local-variable">this</span>.lang = lang;
  1436. }
  1437. <span class="directive">public</span> <span class="predefined-type">String</span> getDescription() {
  1438. <span class="keyword">return</span> description;
  1439. }
  1440. <span class="directive">public</span> <span class="type">void</span> setDescription(<span class="predefined-type">String</span> description) {
  1441. <span class="local-variable">this</span>.description = description;
  1442. }
  1443. <span class="directive">public</span> <span class="predefined-type">String</span> getKeywords() {
  1444. <span class="keyword">return</span> keywords;
  1445. }
  1446. <span class="directive">public</span> <span class="type">void</span> setKeywords(<span class="predefined-type">String</span> keywords) {
  1447. <span class="local-variable">this</span>.keywords = keywords;
  1448. }
  1449. <span class="directive">public</span> Code getCode() {
  1450. <span class="keyword">return</span> code;
  1451. }
  1452. <span class="directive">public</span> <span class="type">void</span> setCode(Code code) {
  1453. <span class="local-variable">this</span>.code = code;
  1454. }
  1455. <span class="directive">public</span> <span class="predefined-type">Date</span> getCreated() {
  1456. <span class="keyword">return</span> created;
  1457. }
  1458. <span class="directive">public</span> <span class="type">void</span> setCreated(<span class="predefined-type">Date</span> created) {
  1459. <span class="local-variable">this</span>.created = created;
  1460. }
  1461. <span class="directive">public</span> <span class="predefined-type">Date</span> getModified() {
  1462. <span class="keyword">return</span> modified;
  1463. }
  1464. <span class="directive">public</span> <span class="type">void</span> setModified(<span class="predefined-type">Date</span> modified) {
  1465. <span class="local-variable">this</span>.modified = modified;
  1466. }
  1467. <span class="annotation">@PrePersist</span>
  1468. <span class="type">void</span> onCreate() {
  1469. <span class="local-variable">this</span>.setCreated(<span class="keyword">new</span> <span class="predefined-type">Date</span>());
  1470. <span class="local-variable">this</span>.setModified(<span class="keyword">new</span> <span class="predefined-type">Date</span>());
  1471. }
  1472. <span class="annotation">@PreUpdate</span>
  1473. <span class="type">void</span> onUpdate() {
  1474. <span class="local-variable">this</span>.setModified(<span class="keyword">new</span> <span class="predefined-type">Date</span>());
  1475. }
  1476. }</code></pre>
  1477. </div>
  1478. </div>
  1479. <div class="paragraph">
  1480. <p>See that you are using now the <strong>JPA</strong> standard annotations: <strong>@Entity</strong>, <strong>@Id</strong>, <strong>@OneToOne</strong>, etc.</p>
  1481. </div>
  1482. </li>
  1483. <li>
  1484. <p>The repositories will change becasue you will use the power of the <strong>spring-data</strong> and <strong>spring-data-jpa</strong> projects. Create/Modify the following classes:</p>
  1485. <div class="listingblock">
  1486. <div class="title">io.pivotal.workshop.snippet.repository.LanguageRepository.java</div>
  1487. <div class="content">
  1488. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository</span>;
  1489. <span class="keyword">import</span> <span class="include">org.springframework.data.repository.CrudRepository</span>;
  1490. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Language</span>;
  1491. <span class="directive">public</span> <span class="type">interface</span> <span class="class">LanguageRepository</span> <span class="directive">extends</span> CrudRepository&lt;Language, <span class="predefined-type">String</span>&gt; {
  1492. }</code></pre>
  1493. </div>
  1494. </div>
  1495. <div class="listingblock">
  1496. <div class="title">io.pivotal.workshop.snippet.repository.CodeRepository.java</div>
  1497. <div class="content">
  1498. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository</span>;
  1499. <span class="keyword">import</span> <span class="include">org.springframework.data.repository.CrudRepository</span>;
  1500. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Code</span>;
  1501. <span class="directive">public</span> <span class="type">interface</span> <span class="class">CodeRepository</span> <span class="directive">extends</span> CrudRepository&lt;Code, <span class="predefined-type">String</span>&gt; {
  1502. }</code></pre>
  1503. </div>
  1504. </div>
  1505. <div class="listingblock">
  1506. <div class="title">io.pivotal.workshop.snippet.repository.SnippetRepository.java</div>
  1507. <div class="content">
  1508. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.repository</span>;
  1509. <span class="keyword">import</span> <span class="include">org.springframework.data.repository.CrudRepository</span>;
  1510. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.domain.Snippet</span>;
  1511. <span class="directive">public</span> <span class="type">interface</span> <span class="class">SnippetRepository</span> <span class="directive">extends</span> CrudRepository&lt;Snippet, <span class="predefined-type">String</span>&gt; {
  1512. }</code></pre>
  1513. </div>
  1514. </div>
  1515. <div class="paragraph">
  1516. <p>As you can see from the code above, you don&#8217;t need to implement anything but just extend from the <strong>CrudRepository&lt;T,ID&gt;</strong> interface, which uses generics, meaning that you need to
  1517. pass the domain class and the unique identifies, in this case what is marked as <strong>@Id</strong>. All the operation from the <strong>CrudRepository&lt;T,ID&gt;</strong> interface will be implemented by the <strong>spring-data</strong> classes.</p>
  1518. </div>
  1519. </li>
  1520. <li>
  1521. <p>The <strong>SnippetController</strong> and the <strong>SnippetConfiguration</strong> classes must be the same. Practically there is no change on any of them, they should work.</p>
  1522. </li>
  1523. <li>
  1524. <p>This time there is no need for any <strong>SQL</strong> schema, this time you will use the <strong><em>DDL auto-creation</em></strong> feature. In the <strong>src/main/resources/application.properties</strong> file add the following content:</p>
  1525. <div class="listingblock">
  1526. <div class="title">src/main/resources/application.properties</div>
  1527. <div class="content">
  1528. <pre class="CodeRay highlight"><code data-lang="properties">## JPA
  1529. spring.jpa.generate-ddl=true
  1530. spring.jpa.hibernate.ddl-auto=create-drop</code></pre>
  1531. </div>
  1532. </div>
  1533. </li>
  1534. </ol>
  1535. </div>
  1536. <div class="admonitionblock tip">
  1537. <table>
  1538. <tr>
  1539. <td class="icon">
  1540. <div class="title">Tip</div>
  1541. </td>
  1542. <td class="content">
  1543. Before you run your application make sure to have copied <strong>resources/static</strong> and <strong>resources/templates</strong> files from the previous labs.
  1544. </td>
  1545. </tr>
  1546. </table>
  1547. </div>
  1548. <div class="sect2">
  1549. <h3 id="_challenges_2">Challenges</h3>
  1550. <div class="ulist">
  1551. <ul>
  1552. <li>
  1553. <p>Run the application and make sure it works.</p>
  1554. </li>
  1555. <li>
  1556. <p>Do any necessary change to have the home page working.</p>
  1557. <div class="paragraph">
  1558. <div class="title">Figure 1.1: Code Snippet Manager JPA - <a href="http://localhost:8080/" class="bare" target="_blank">http://localhost:8080/</a></div>
  1559. <p><span class="image"><img src="images/06-spring-boot-data-04.png" alt="Code Snippet Manager JPA" title="Code Snippet Manager JPA"></span></p>
  1560. </div>
  1561. </li>
  1562. </ul>
  1563. </div>
  1564. </div>
  1565. </div>
  1566. </div>
  1567. <div class="sect1">
  1568. <h2 id="_code_snippet_manager_jpa_rest">Code Snippet Manager JPA REST</h2>
  1569. <div class="sectionbody">
  1570. <div class="paragraph">
  1571. <p>You will continue with the <strong>Code Snippet Manager</strong> code, but this time using <strong>Data Rest</strong> module. You will reuse the code from previous labs.</p>
  1572. </div>
  1573. <div class="olist arabic">
  1574. <ol class="arabic">
  1575. <li>
  1576. <p>Open a browser and hit the url: <a href="http://start.spring.io" class="bare" target="_blank">http://start.spring.io</a></p>
  1577. </li>
  1578. <li>
  1579. <p>Click the <strong><em>Switch to the full version</em></strong> link.</p>
  1580. </li>
  1581. <li>
  1582. <p>Fill out the <strong>Code Snippet Manager JPA REST</strong> Project metadata with (See Figure 3.0):</p>
  1583. <table class="tableblock frame-all grid-all spread">
  1584. <caption class="title">Table 3. Code Snippet Manager JPA REST App - metadata</caption>
  1585. <colgroup>
  1586. <col style="width: 16.6666%;">
  1587. <col style="width: 83.3334%;">
  1588. </colgroup>
  1589. <thead>
  1590. <tr>
  1591. <th class="tableblock halign-left valign-top">Property</th>
  1592. <th class="tableblock halign-left valign-top">Value</th>
  1593. </tr>
  1594. </thead>
  1595. <tbody>
  1596. <tr>
  1597. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Group</strong>:</p></td>
  1598. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>io.pivotal.workshop</em></p></td>
  1599. </tr>
  1600. <tr>
  1601. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Artifact</strong>:</p></td>
  1602. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>code-snippet-manager-jpa-rest</em></p></td>
  1603. </tr>
  1604. <tr>
  1605. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Name</strong>:</p></td>
  1606. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>code-snippet-manager-jpa-rest</em></p></td>
  1607. </tr>
  1608. <tr>
  1609. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Package Name</strong>:</p></td>
  1610. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>io.pivotal.workshop.snippet</em></p></td>
  1611. </tr>
  1612. <tr>
  1613. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Dependencies</strong>:</p></td>
  1614. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>Web, DevTools, Groovy Templates, JPA, Rest Repositories, H2, MySQL</em></p></td>
  1615. </tr>
  1616. <tr>
  1617. <td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Spring Boot</strong>:</p></td>
  1618. <td class="tableblock halign-left valign-top"><p class="tableblock"><em>2.0.0.M7</em></p></td>
  1619. </tr>
  1620. </tbody>
  1621. </table>
  1622. <div class="paragraph">
  1623. <div class="title">Figure 3.0: Spring Initializr - <a href="http://start.spring.io/" class="bare" target="_blank">http://start.spring.io/</a></div>
  1624. <p><span class="image"><img src="images/06-spring-boot-data-03.png" alt="SpringInitializr" title="Spring Initializr"></span></p>
  1625. </div>
  1626. <div class="admonitionblock tip">
  1627. <table>
  1628. <tr>
  1629. <td class="icon">
  1630. <div class="title">Tip</div>
  1631. </td>
  1632. <td class="content">
  1633. You can choose either <strong>Maven</strong> or <strong>Gradle</strong> project types.
  1634. </td>
  1635. </tr>
  1636. </table>
  1637. </div>
  1638. </li>
  1639. <li>
  1640. <p>type <strong>Web</strong>, <strong>DevTools</strong>, <strong>Groovy Templates</strong>, <strong>JPA</strong>, <strong>Rest Repositories</strong>, <strong>H2</strong>, and <strong>MySQL</strong> in the <strong><em>Dependencies</em></strong> field and press Enter.</p>
  1641. </li>
  1642. <li>
  1643. <p>Click the <strong><em>Generate Project</em></strong> button.</p>
  1644. </li>
  1645. <li>
  1646. <p>Unzip the file in any directory you want.</p>
  1647. </li>
  1648. <li>
  1649. <p>Import your project in any IDE you want.</p>
  1650. </li>
  1651. <li>
  1652. <p>Copy all the packages (with code) into the new project.</p>
  1653. </li>
  1654. <li>
  1655. <p>The only class that will change will be the <strong>SnippetController</strong>. Create/modify the folowing class:</p>
  1656. <div class="listingblock">
  1657. <div class="title">io.pivotal.workshop.snippet.controller.SnippetController.java</div>
  1658. <div class="content">
  1659. <pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">io.pivotal.workshop.snippet.controller</span>;
  1660. <span class="keyword">import</span> <span class="include">java.util.HashMap</span>;
  1661. <span class="keyword">import</span> <span class="include">java.util.Map</span>;
  1662. <span class="keyword">import</span> <span class="include">org.springframework.web.bind.annotation.RequestMapping</span>;
  1663. <span class="keyword">import</span> <span class="include">org.springframework.web.bind.annotation.RestController</span>;
  1664. <span class="keyword">import</span> <span class="include">org.springframework.web.servlet.ModelAndView</span>;
  1665. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.repository.LanguageRepository</span>;
  1666. <span class="keyword">import</span> <span class="include">io.pivotal.workshop.snippet.repository.SnippetRepository</span>;
  1667. <span class="annotation">@RestController</span>
  1668. <span class="directive">public</span> <span class="type">class</span> <span class="class">SnippetController</span> {
  1669. <span class="directive">private</span> SnippetRepository snippetRepository;
  1670. <span class="directive">private</span> LanguageRepository languageRepository;
  1671. <span class="directive">public</span> SnippetController(SnippetRepository snippetRepository,LanguageRepository languageRepository){
  1672. <span class="local-variable">this</span>.snippetRepository = snippetRepository;
  1673. <span class="local-variable">this</span>.languageRepository = languageRepository;
  1674. }
  1675. <span class="annotation">@RequestMapping</span>(<span class="string"><span class="delimiter">&quot;</span><span class="content">/</span><span class="delimiter">&quot;</span></span>)
  1676. <span class="directive">public</span> ModelAndView home(){
  1677. <span class="keyword">assert</span> snippetRepository != <span class="predefined-constant">null</span>;
  1678. <span class="predefined-type">Map</span>&lt;<span class="predefined-type">String</span>,<span class="predefined-type">Object</span>&gt; model = <span class="keyword">new</span> <span class="predefined-type">HashMap</span>&lt;<span class="predefined-type">String</span>,<span class="predefined-type">Object</span>&gt;();
  1679. model.put(<span class="string"><span class="delimiter">&quot;</span><span class="content">langs</span><span class="delimiter">&quot;</span></span>, languageRepository.findAll());
  1680. model.put(<span class="string"><span class="delimiter">&quot;</span><span class="content">snippets</span><span class="delimiter">&quot;</span></span>, snippetRepository.findAll());
  1681. <span class="keyword">return</span> <span class="keyword">new</span> ModelAndView(<span class="string"><span class="delimiter">&quot;</span><span class="content">views/home</span><span class="delimiter">&quot;</span></span>,model);
  1682. }
  1683. }</code></pre>
  1684. </div>
  1685. </div>
  1686. <div class="paragraph">
  1687. <p>As you can see, only is necessary the <strong>home</strong> method, and this is because the <strong>spring-data-rest</strong> module will take care of creating all the web controllers for accepting any request regarding to the domain repositories.</p>
  1688. </div>
  1689. </li>
  1690. <li>
  1691. <p>When using the <strong>spring-data-rest</strong> libraries, the default path for the controllers is the root: <strong>/</strong>, but in this case you are using the root as home page, that&#8217;s why is necessary to override the <strong>spring-data-rest</strong> context path defaults,
  1692. in the <strong>src/main/resources/application.properties</strong> file add the following:</p>
  1693. <div class="listingblock">
  1694. <div class="title">src/main/resources/application.properties</div>
  1695. <div class="content">
  1696. <pre class="CodeRay highlight"><code data-lang="properties">## JPA
  1697. spring.jpa.generate-ddl=true
  1698. spring.jpa.hibernate.ddl-auto=create-drop
  1699. ## REST
  1700. spring.data.rest.base-path=api</code></pre>
  1701. </div>
  1702. </div>
  1703. <div class="paragraph">
  1704. <p>Now you should be able to use the <a href="http://localhost:8080/api" class="bare" target="_blank">http://localhost:8080/api</a> to access the REST repositories.</p>
  1705. </div>
  1706. </li>
  1707. </ol>
  1708. </div>
  1709. <div class="admonitionblock tip">
  1710. <table>
  1711. <tr>
  1712. <td class="icon">
  1713. <div class="title">Tip</div>
  1714. </td>
  1715. <td class="content">
  1716. Before you run your application make sure to have copied <strong>resources/static</strong> and <strong>resources/templates</strong> files from the previous labs.
  1717. </td>
  1718. </tr>
  1719. </table>
  1720. </div>
  1721. <div class="sect2">
  1722. <h3 id="_challenges_3">Challenges</h3>
  1723. <div class="ulist">
  1724. <ul>
  1725. <li>
  1726. <p>Run the application and make sure it works.</p>
  1727. <div class="admonitionblock tip">
  1728. <table>
  1729. <tr>
  1730. <td class="icon">
  1731. <div class="title">Tip</div>
  1732. </td>
  1733. <td class="content">
  1734. See the logs and take a look at all the mapping that is being generated.
  1735. </td>
  1736. </tr>
  1737. </table>
  1738. </div>
  1739. </li>
  1740. <li>
  1741. <p>Do any necessary change to have the home page working.</p>
  1742. <div class="paragraph">
  1743. <div class="title">Figure 3.1: Code Snippet Manager JPA REST - <a href="http://localhost:8080/" class="bare" target="_blank">http://localhost:8080/</a></div>
  1744. <p><span class="image"><img src="images/06-spring-boot-data-04.png" alt="Code Snippet Manager" title="Code Snippet Manager"></span></p>
  1745. </div>
  1746. </li>
  1747. <li>
  1748. <p>Go to the <a href="http://localhost:8080/api" class="bare" target="_blank">http://localhost:8080/api</a>, you should see the following image:</p>
  1749. <div class="paragraph">
  1750. <div class="title">Figure 3.2: Code Snippet Manager JPA REST - <a href="http://localhost:8080/api" class="bare" target="_blank">http://localhost:8080/api</a></div>
  1751. <p><span class="image"><img src="images/06-spring-boot-data-05.png" alt="Code Snippet Manager API" title="Code Snippet Manager API"></span></p>
  1752. </div>
  1753. <div class="paragraph">
  1754. <p>and if you click the <a href="http://localhost:8080/api/snippets" class="bare" target="_blank">http://localhost:8080/api/snippets</a> you should see the following image:</p>
  1755. </div>
  1756. <div class="paragraph">
  1757. <div class="title">Figure 3.2: Code Snippet Manager JPA REST - <a href="http://localhost:8080/api/snippets" class="bare" target="_blank">http://localhost:8080/api/snippets</a></div>
  1758. <p><span class="image"><img src="images/06-spring-boot-data-06.png" alt="Code Snippet Manager API" title="Code Snippet Manager API"></span></p>
  1759. </div>
  1760. </li>
  1761. <li>
  1762. <p>Review all the links and see that the all the information is being translated into a <strong>JSON/HAL</strong>.</p>
  1763. </li>
  1764. <li>
  1765. <p>So far we are using the <strong>H2</strong> engine, do the necessary changes to use now <strong>MySQL</strong>, how can you accomplish this?</p>
  1766. </li>
  1767. </ul>
  1768. </div>
  1769. </div>
  1770. </div>
  1771. </div>
  1772. <div class="sect1">
  1773. <h2 id="_homework">HOMEWORK</h2>
  1774. <div class="sectionbody">
  1775. <div class="ulist">
  1776. <ul>
  1777. <li>
  1778. <p>Use multiple databases like MySQL and MongoDB (multiple Datasources).</p>
  1779. </li>
  1780. <li>
  1781. <p>Use <strong>Flyway</strong> or <strong>Liquibase</strong> to do a Database migration. Add an <strong><em>executeCommand</em></strong> field to the <strong>Code</strong> domain class. This field will tell how to execute the source code provided.</p>
  1782. </li>
  1783. <li>
  1784. <p>Use <strong>jOOQ</strong> framework.</p>
  1785. </li>
  1786. </ul>
  1787. </div>
  1788. </div>
  1789. </div>
  1790. </div>
  1791. <div id="footer">
  1792. <div id="footer-text">
  1793. Last updated 2017-11-18 11:05:24 PST
  1794. </div>
  1795. </div>
  1796. </body>
  1797. </html>