{"id":110,"date":"2023-12-04T22:25:51","date_gmt":"2023-12-05T03:25:51","guid":{"rendered":"https:\/\/coding101.xyz\/?p=110"},"modified":"2023-12-06T12:29:33","modified_gmt":"2023-12-06T17:29:33","slug":"sample-jpa-spring-boot","status":"publish","type":"post","link":"https:\/\/coding101.xyz\/?p=110","title":{"rendered":"Sample JPA Spring Boot"},"content":{"rendered":"\n<p>This is a sample of how to access the database from spring boot using JPA. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Here is the database structure: <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"842\" height=\"776\" src=\"https:\/\/coding101.xyz\/wp-content\/uploads\/2023\/12\/image.png\" alt=\"\" class=\"wp-image-111\" style=\"width:324px;height:auto\"\/><\/figure>\n\n\n\n<p>Note a couple of things:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>One to many first -&gt; second<\/li>\n\n\n\n<li>The field in the second table that links to the first is first_id<\/li>\n\n\n\n<li>There is a foreign key constraint from second to the first<\/li>\n<\/ul>\n\n\n\n<p>OK, now the application. Here are the maven dependencies (basically just add spring-java-jpa and that is it)<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><table><tr><td><pre style=\"margin: 0; line-height: 125%\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18<\/pre><\/td><td><pre style=\"margin: 0; line-height: 125%\">    <span style=\"color: #000080; font-weight: bold\">&lt;dependencies&gt;<\/span>\n        <span style=\"color: #000080; font-weight: bold\">&lt;dependency&gt;<\/span>\n            <span style=\"color: #000080; font-weight: bold\">&lt;groupId&gt;<\/span>org.springframework.boot<span style=\"color: #000080; font-weight: bold\">&lt;\/groupId&gt;<\/span>\n            <span style=\"color: #008800; font-style: italic\">&lt;!-- for starting with web, use starter-web --&gt;<\/span>\n            <span style=\"color: #000080; font-weight: bold\">&lt;artifactId&gt;<\/span>spring-boot-starter<span style=\"color: #000080; font-weight: bold\">&lt;\/artifactId&gt;<\/span>\n        <span style=\"color: #000080; font-weight: bold\">&lt;\/dependency&gt;<\/span>\n        <span style=\"color: #000080; font-weight: bold\">&lt;dependency&gt;<\/span>\n            <span style=\"color: #000080; font-weight: bold\">&lt;groupId&gt;<\/span>org.springframework.boot<span style=\"color: #000080; font-weight: bold\">&lt;\/groupId&gt;<\/span>\n            <span style=\"color: #008800; font-style: italic\">&lt;!-- for starting with web, use starter-web --&gt;<\/span>\n            <span style=\"color: #000080; font-weight: bold\">&lt;artifactId&gt;<\/span>spring-boot-starter-data-jpa<span style=\"color: #000080; font-weight: bold\">&lt;\/artifactId&gt;<\/span>\n        <span style=\"color: #000080; font-weight: bold\">&lt;\/dependency&gt;<\/span>\n        <span style=\"color: #008800; font-style: italic\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.mariadb.jdbc\/mariadb-java-client --&gt;<\/span>\n        <span style=\"color: #000080; font-weight: bold\">&lt;dependency&gt;<\/span>\n            <span style=\"color: #000080; font-weight: bold\">&lt;groupId&gt;<\/span>org.mariadb.jdbc<span style=\"color: #000080; font-weight: bold\">&lt;\/groupId&gt;<\/span>\n            <span style=\"color: #000080; font-weight: bold\">&lt;artifactId&gt;<\/span>mariadb-java-client<span style=\"color: #000080; font-weight: bold\">&lt;\/artifactId&gt;<\/span>\n            <span style=\"color: #000080; font-weight: bold\">&lt;version&gt;<\/span>3.1.4<span style=\"color: #000080; font-weight: bold\">&lt;\/version&gt;<\/span>\n        <span style=\"color: #000080; font-weight: bold\">&lt;\/dependency&gt;<\/span>\n    <span style=\"color: #000080; font-weight: bold\">&lt;\/dependencies&gt;<\/span>\n<\/pre><\/td><\/tr><\/table><\/div>\n\n\n\n<p>The entity classes are FirstDt and SecondDt, and here they are: <\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><table><tr><td><pre style=\"margin: 0; line-height: 125%\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65<\/pre><\/td><td><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #000080; font-weight: bold\">package<\/span> org.<span style=\"color: #FF0000\">example<\/span>.<span style=\"color: #FF0000\">dt<\/span>;\n\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.*;\n\n<span style=\"color: #000080; font-weight: bold\">import<\/span> java.util.ArrayList;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> java.util.List;\n\n@Entity\n@Table(name = <span style=\"color: #0000FF\">&quot;first&quot;<\/span>)\n@NamedQueries(\n        {@NamedQuery(name=<span style=\"color: #0000FF\">&quot;first.query&quot;<\/span>, query=<span style=\"color: #0000FF\">&quot;select a from FirstDt a where a.comments = :cm1&quot;<\/span>)}\n)\n<span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">class<\/span> FirstDt {\n    @Id\n    @Column(name = <span style=\"color: #0000FF\">&quot;first_id&quot;<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> String id;\n\n    @Column(name = <span style=\"color: #0000FF\">&quot;first_coments&quot;<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> String comments;\n\n    @Column(name = <span style=\"color: #0000FF\">&quot;shoe_size&quot;<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> <span style=\"color: #000080; font-weight: bold\">int<\/span> shoeSize;\n\n    @OneToMany(fetch = FetchType.<span style=\"color: #FF0000\">LAZY<\/span>)\n    @JoinColumn(name = <span style=\"color: #0000FF\">&quot;first_id&quot;<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> <span style=\"color: #000080; font-weight: bold\">final<\/span> List&lt;SecondDt&gt; items;\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> FirstDt() {\n        <span style=\"color: #000080; font-weight: bold\">this<\/span>.<span style=\"color: #FF0000\">items<\/span> = <span style=\"color: #000080; font-weight: bold\">new<\/span> ArrayList&lt;&gt;();\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> String getId() {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> id;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">void<\/span> setId(String id) {\n        <span style=\"color: #000080; font-weight: bold\">this<\/span>.<span style=\"color: #FF0000\">id<\/span> = id;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> String getComments() {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> comments;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">void<\/span> setComments(String comments) {\n        <span style=\"color: #000080; font-weight: bold\">this<\/span>.<span style=\"color: #FF0000\">comments<\/span> = comments;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">int<\/span> getShoeSize() {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> shoeSize;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">void<\/span> setShoeSize(<span style=\"color: #000080; font-weight: bold\">int<\/span> shoeSize) {\n        <span style=\"color: #000080; font-weight: bold\">this<\/span>.<span style=\"color: #FF0000\">shoeSize<\/span> = shoeSize;\n    }\n\n    @Override\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> String toString() {\n        <span style=\"color: #000080; font-weight: bold\">final<\/span> StringBuilder sb = <span style=\"color: #000080; font-weight: bold\">new<\/span> StringBuilder(<span style=\"color: #0000FF\">&quot;FirstDt{&quot;<\/span>);\n        sb.<span style=\"color: #FF0000\">append<\/span>(<span style=\"color: #0000FF\">&quot;id=&#39;&quot;<\/span>).<span style=\"color: #FF0000\">append<\/span>(id).<span style=\"color: #FF0000\">append<\/span>(<span style=\"color: #800080\">&#39;\\&#39;&#39;<\/span>);\n        sb.<span style=\"color: #FF0000\">append<\/span>(<span style=\"color: #0000FF\">&quot;, comments=&#39;&quot;<\/span>).<span style=\"color: #FF0000\">append<\/span>(comments).<span style=\"color: #FF0000\">append<\/span>(<span style=\"color: #800080\">&#39;\\&#39;&#39;<\/span>);\n        sb.<span style=\"color: #FF0000\">append<\/span>(<span style=\"color: #0000FF\">&quot;, shoeSize=&quot;<\/span>).<span style=\"color: #FF0000\">append<\/span>(shoeSize);\n        sb.<span style=\"color: #FF0000\">append<\/span>(<span style=\"color: #800080\">&#39;}&#39;<\/span>);\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> sb.<span style=\"color: #FF0000\">toString<\/span>();\n    }\n}\n<\/pre><\/td><\/tr><\/table><\/div>\n\n\n\n<p>And second dt is here:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><table><tr><td><pre style=\"margin: 0; line-height: 125%\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48<\/pre><\/td><td><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #000080; font-weight: bold\">package<\/span> org.<span style=\"color: #FF0000\">example<\/span>.<span style=\"color: #FF0000\">dt<\/span>;\n\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.Column;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.Entity;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.Id;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.Table;\n\n@Entity\n@Table(name = <span style=\"color: #0000FF\">&quot;second&quot;<\/span>)\n<span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">class<\/span> SecondDt {\n\n    @Id\n    @Column(name = <span style=\"color: #0000FF\">&quot;second_id&quot;<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> String id;\n\n    @Column(name = <span style=\"color: #0000FF\">&quot;second_info&quot;<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> String info;\n\n    @Column(name = <span style=\"color: #0000FF\">&quot;salary&quot;<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> <span style=\"color: #000080; font-weight: bold\">double<\/span> salary;\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> SecondDt() {\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> String getId() {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> id;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">void<\/span> setId(String id) {\n        <span style=\"color: #000080; font-weight: bold\">this<\/span>.<span style=\"color: #FF0000\">id<\/span> = id;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> String getInfo() {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> info;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">void<\/span> setInfo(String info) {\n        <span style=\"color: #000080; font-weight: bold\">this<\/span>.<span style=\"color: #FF0000\">info<\/span> = info;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">double<\/span> getSalary() {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> salary;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">void<\/span> setSalary(<span style=\"color: #000080; font-weight: bold\">double<\/span> salary) {\n        <span style=\"color: #000080; font-weight: bold\">this<\/span>.<span style=\"color: #FF0000\">salary<\/span> = salary;\n    }\n}\n<\/pre><\/td><\/tr><\/table><\/div>\n\n\n\n<p>And this is the service that is used to access through those<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><table><tr><td><pre style=\"margin: 0; line-height: 125%\">  1\n  2\n  3\n  4\n  5\n  6\n  7\n  8\n  9\n 10\n 11\n 12\n 13\n 14\n 15\n 16\n 17\n 18\n 19\n 20\n 21\n 22\n 23\n 24\n 25\n 26\n 27\n 28\n 29\n 30\n 31\n 32\n 33\n 34\n 35\n 36\n 37\n 38\n 39\n 40\n 41\n 42\n 43\n 44\n 45\n 46\n 47\n 48\n 49\n 50\n 51\n 52\n 53\n 54\n 55\n 56\n 57\n 58\n 59\n 60\n 61\n 62\n 63\n 64\n 65\n 66\n 67\n 68\n 69\n 70\n 71\n 72\n 73\n 74\n 75\n 76\n 77\n 78\n 79\n 80\n 81\n 82\n 83\n 84\n 85\n 86\n 87\n 88\n 89\n 90\n 91\n 92\n 93\n 94\n 95\n 96\n 97\n 98\n 99\n100\n101\n102\n103\n104\n105\n106\n107\n108<\/pre><\/td><td><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #000080; font-weight: bold\">package<\/span> org.<span style=\"color: #FF0000\">example<\/span>.<span style=\"color: #FF0000\">service<\/span>;\n\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.EntityManager;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.PersistenceContext;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.Query;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.persistence.TypedQuery;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> jakarta.transaction.Transactional;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> org.example.dt.FirstDt;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> org.example.persist.IFirstRepo;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> org.springframework.beans.factory.annotation.Autowired;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> org.springframework.data.domain.Sort;\n<span style=\"color: #000080; font-weight: bold\">import<\/span> org.springframework.stereotype.Component;\n\n<span style=\"color: #000080; font-weight: bold\">import<\/span> java.util.List;\n\n@Component\n<span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">class<\/span> InfoService {\n\n    @PersistenceContext\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> EntityManager em;\n\n    @Autowired\n    <span style=\"color: #000080; font-weight: bold\">private<\/span> IFirstRepo firstRepo;\n\n    @Transactional(Transactional.<span style=\"color: #FF0000\">TxType<\/span>.<span style=\"color: #FF0000\">REQUIRED<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> String createFirst() <span style=\"color: #000080; font-weight: bold\">throws<\/span> Exception {\n        FirstDt first = <span style=\"color: #000080; font-weight: bold\">new<\/span> FirstDt();\n        first.<span style=\"color: #FF0000\">setId<\/span>(<span style=\"color: #0000FF\">&quot;id4&quot;<\/span>);\n        first.<span style=\"color: #FF0000\">setComments<\/span>(<span style=\"color: #0000FF\">&quot;the comments for id4&quot;<\/span>);\n        first.<span style=\"color: #FF0000\">setShoeSize<\/span>(<span style=\"color: #0000FF\">25<\/span>);\n\n        em.<span style=\"color: #FF0000\">persist<\/span>(first);\n\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> <span style=\"color: #0000FF\">&quot;current&quot;<\/span>;\n    }\n\n    @Transactional(Transactional.<span style=\"color: #FF0000\">TxType<\/span>.<span style=\"color: #FF0000\">REQUIRED<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> List&lt;FirstDt&gt; getAllFirst() {\n        List&lt;FirstDt&gt; items = firstRepo.<span style=\"color: #FF0000\">findAll<\/span>();\n\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> items;\n    }\n\n    @Transactional(Transactional.<span style=\"color: #FF0000\">TxType<\/span>.<span style=\"color: #FF0000\">REQUIRED<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> List&lt;FirstDt&gt; getAllSorted() {\n        List&lt;FirstDt&gt; items = firstRepo.<span style=\"color: #FF0000\">findAll<\/span>(Sort.<span style=\"color: #FF0000\">by<\/span>(Sort.<span style=\"color: #FF0000\">Direction<\/span>.<span style=\"color: #FF0000\">DESC<\/span>, <span style=\"color: #0000FF\">&quot;shoeSize&quot;<\/span>).<span style=\"color: #FF0000\">and<\/span>(Sort.<span style=\"color: #FF0000\">by<\/span>(Sort.<span style=\"color: #FF0000\">Direction<\/span>.<span style=\"color: #FF0000\">ASC<\/span>, <span style=\"color: #0000FF\">&quot;comments&quot;<\/span>)));\n\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> items;\n    }\n\n    @Transactional(Transactional.<span style=\"color: #FF0000\">TxType<\/span>.<span style=\"color: #FF0000\">REQUIRED<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> List&lt;FirstDt&gt; getThatOne(String id) {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> firstRepo.<span style=\"color: #FF0000\">getThatOne<\/span>(id);\n    }\n\n    @Transactional(Transactional.<span style=\"color: #FF0000\">TxType<\/span>.<span style=\"color: #FF0000\">REQUIRED<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">double<\/span> getShoeSum() {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> firstRepo.<span style=\"color: #FF0000\">getSum<\/span>();\n    }\n\n    @Transactional(Transactional.<span style=\"color: #FF0000\">TxType<\/span>.<span style=\"color: #FF0000\">REQUIRED<\/span>)\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> <span style=\"color: #000080; font-weight: bold\">double<\/span> getNativeShoeSum() {\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> firstRepo.<span style=\"color: #FF0000\">getNativeSum<\/span>();\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> FirstDt searchThatFirst() {\n        TypedQuery&lt;FirstDt&gt; query = em.<span style=\"color: #FF0000\">createQuery<\/span>(<span style=\"color: #0000FF\">&quot;select a from FirstDt a where a.id = :itemId&quot;<\/span>, FirstDt.<span style=\"color: #FF0000\">class<\/span>);\n        query.<span style=\"color: #FF0000\">setParameter<\/span>(<span style=\"color: #0000FF\">&quot;itemId&quot;<\/span>, <span style=\"color: #0000FF\">&quot;id1&quot;<\/span>);\n        List&lt;FirstDt&gt; items = query.<span style=\"color: #FF0000\">getResultList<\/span>();\n\n        FirstDt res = <span style=\"color: #000080; font-weight: bold\">null<\/span>;\n        <span style=\"color: #000080; font-weight: bold\">if<\/span> (items.<span style=\"color: #FF0000\">size<\/span>() &gt; <span style=\"color: #0000FF\">0<\/span>) {\n            res = items.<span style=\"color: #FF0000\">get<\/span>(<span style=\"color: #0000FF\">0<\/span>);\n        }\n\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> res;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> FirstDt searchId() {\n        FirstDt res = <span style=\"color: #000080; font-weight: bold\">null<\/span>;\n\n        TypedQuery&lt;FirstDt&gt; query = em.<span style=\"color: #FF0000\">createNamedQuery<\/span>(<span style=\"color: #0000FF\">&quot;first.query&quot;<\/span>, FirstDt.<span style=\"color: #FF0000\">class<\/span>);\n        query.<span style=\"color: #FF0000\">setParameter<\/span>(<span style=\"color: #0000FF\">&quot;cm1&quot;<\/span>, <span style=\"color: #0000FF\">&quot;the comments for id2&quot;<\/span>);\n        List&lt;FirstDt&gt; items = query.<span style=\"color: #FF0000\">getResultList<\/span>();\n\n        <span style=\"color: #000080; font-weight: bold\">if<\/span> (items.<span style=\"color: #FF0000\">size<\/span>() &gt; <span style=\"color: #0000FF\">0<\/span>) {\n            res = items.<span style=\"color: #FF0000\">get<\/span>(<span style=\"color: #0000FF\">0<\/span>);\n        }\n\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> res;\n    }\n\n    <span style=\"color: #000080; font-weight: bold\">public<\/span> FirstDt getAllFirst(String id) {\n        TypedQuery&lt;FirstDt&gt; query = em.<span style=\"color: #FF0000\">createQuery<\/span>(<span style=\"color: #0000FF\">&quot;select f from FirstDt f left join fetch f.items where f.id = :id&quot;<\/span>, FirstDt.<span style=\"color: #FF0000\">class<\/span>);\n        query.<span style=\"color: #FF0000\">setParameter<\/span>(<span style=\"color: #0000FF\">&quot;id&quot;<\/span>, id);\n\n        List&lt;FirstDt&gt; items = query.<span style=\"color: #FF0000\">getResultList<\/span>();\n        FirstDt res = <span style=\"color: #000080; font-weight: bold\">null<\/span>;\n\n        <span style=\"color: #000080; font-weight: bold\">if<\/span> (items.<span style=\"color: #FF0000\">size<\/span>() &gt; <span style=\"color: #0000FF\">0<\/span>) {\n            res = items.<span style=\"color: #FF0000\">get<\/span>(<span style=\"color: #0000FF\">0<\/span>);\n        }\n\n        <span style=\"color: #000080; font-weight: bold\">return<\/span> res;\n\n    }\n\n}\n<\/pre><\/td><\/tr><\/table><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is a sample of how to access the database from spring boot using JPA.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":2,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}