{"id":60,"date":"2019-06-30T17:18:41","date_gmt":"2019-06-30T17:18:41","guid":{"rendered":"https:\/\/openapex.org\/spaces\/software\/?p=60"},"modified":"2022-02-06T13:54:14","modified_gmt":"2022-02-06T08:24:14","slug":"spring-boot-app-connecting-to-mongodb-using-jpa","status":"publish","type":"post","link":"https:\/\/mutesoft.com\/spaces\/software\/spring-boot-app-connecting-to-mongodb-using-jpa.html","title":{"rendered":"MongoDB Spring Example"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"492\" src=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/01\/SpringBoot-JPA-MongoDB-1024x492.png\" alt=\"SpringBoot app with MongoDB database using JPA\" class=\"wp-image-951\" srcset=\"https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/01\/SpringBoot-JPA-MongoDB-1024x492.png 1024w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/01\/SpringBoot-JPA-MongoDB-300x144.png 300w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/01\/SpringBoot-JPA-MongoDB-768x369.png 768w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/01\/SpringBoot-JPA-MongoDB-1536x738.png 1536w, https:\/\/mutesoft.com\/spaces\/software\/wp-content\/uploads\/sites\/7\/2022\/01\/SpringBoot-JPA-MongoDB.png 1612w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>SpringBoot app with MongoDB database using JPA<\/figcaption><\/figure><\/div>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_79_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/mutesoft.com\/spaces\/software\/spring-boot-app-connecting-to-mongodb-using-jpa.html\/#1_Context\" >1. Context<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/mutesoft.com\/spaces\/software\/spring-boot-app-connecting-to-mongodb-using-jpa.html\/#2_JPA_Repository_and_Document\" >2. JPA Repository and Document<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/mutesoft.com\/spaces\/software\/spring-boot-app-connecting-to-mongodb-using-jpa.html\/#3_Database_Configuration\" >3. Database Configuration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/mutesoft.com\/spaces\/software\/spring-boot-app-connecting-to-mongodb-using-jpa.html\/#4_Conclusion\" >4. Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"1-context\"><span class=\"ez-toc-section\" id=\"1_Context\"><\/span><strong>1. Context<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this <a href=\"https:\/\/openapex.org\/spaces\/software\/spring-boot-app-connecting-to-postgresql-using-jpa\/\">article<\/a>, we have seen steps of creating a SpringBoot based imaginary TV program guide app that connects to PostgreSQL using JPA. The same code works for any relational database available in the market. We have to use the appropriate database driver and change the database configuration in the application.properties file. Using <code>MongoDB<\/code> in place of <code>PostgreSQL<\/code> is mostly the same. Here are the key differences:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The ORM class is referred here as a document class and the annotation <code>@Document<\/code> is used instead of <code>@Entity<\/code>.<\/li><li> The <code>@Id<\/code> field in the document and entity classes are handled differently <\/li><li>The Repository interface extends from <code>MongoRepositoty<\/code> instead of <code>JpaReposity<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-jpa-repository-and-document\"><span class=\"ez-toc-section\" id=\"2_JPA_Repository_and_Document\"><\/span><strong>2. JPA Repository and Document<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here is the ProgramDocument representing the <code>program <\/code>collection in <code>MongoDB<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package org.openapex.tvguide.model;\n\nimport com.fasterxml.jackson.annotation.JsonIgnore;\nimport lombok.AllArgsConstructor;\nimport lombok.Getter;\nimport lombok.NoArgsConstructor;\nimport lombok.Setter;\nimport org.springframework.data.annotation.Id;\nimport org.springframework.data.mongodb.core.mapping.Document;\n\nimport java.util.List;\n\n@Getter\n@Setter\n@NoArgsConstructor\n@AllArgsConstructor\n@Document(collection = \"program\")\npublic class ProgramDocument {\n    @Id\n    @JsonIgnore\n    private String id;\n    private String title;\n    private String type;\n    private String genre;\n    private String certification;\n    private List&lt;String> actors;\n    private String director;\n    private String producer;\n\n}<\/pre>\n\n\n\n<p>The Repository interface that extends from <code>MongoRepository <\/code>and provides Java APIs for all sorts of database operations.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package org.openapex.tvguide.dbaccess.repository;\n\nimport org.openapex.tvguide.model.ProgramDocument;\nimport org.springframework.data.mongodb.repository.MongoRepository;\nimport org.springframework.stereotype.Repository;\n\n@Repository\npublic interface ProgramRepository extends MongoRepository&lt;ProgramDocument, String> {\n}<\/pre>\n\n\n\n<p>The Controller and service classes are the same whether PostgreSQL is used or a MongoDB used for persistence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-database-configuration\"><span class=\"ez-toc-section\" id=\"3_Database_Configuration\"><\/span><strong>3. Database Configuration<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Database configuration in <code>application.properties<\/code> file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">spring.data.mongodb.host=localhost\nspring.data.mongodb.port=27017\nspring.data.mongodb.database=tvguide<\/pre>\n\n\n\n<p>The database script to create a collection named <code>program <\/code>and add some sample <code>programs<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use tvguide;\ndb.createCollection('program');\ndb.program.insertMany([\n{\n'title':'Casablanca',\n'genre':'Movie',\n},\n{\n'title':'Friends',\n'genre':'Comedy',\n}\n]);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-conclusion\"><span class=\"ez-toc-section\" id=\"4_Conclusion\"><\/span><strong>4. Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Controller and service classes are the same whether <code>PostgreSQL<\/code> is used or <code>MongoDB<\/code> is used for persistence. In addition to channels endpoint, a new REST endpoint is exposed based on program data in <code>MongoDB<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>GET <\/strong><code>\/guide\/programs<\/code> &#8211; lists all programs<\/li><\/ul>\n\n\n\n<p>The full code of this program guide app is available at GitHub:  <a href=\"https:\/\/github.com\/fiveobjects\/reference\/tree\/master\/java\/tvguide\/server\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">tvguide<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Context In this article, we have seen steps of creating a SpringBoot based imaginary TV program guide app that connects to PostgreSQL using JPA. The same code works for any relational database available in the market. We have to<\/p>\n","protected":false},"author":2,"featured_media":951,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"In this article, we&#039;ll create a SpringBoot based imaginary TV program guide app that connects to MongoDB using JPA. The same code works for any NoSQL database available in the market.","_seopress_robots_index":"","_vp_format_video_url":"","_vp_image_focal_point":[],"footnotes":""},"categories":[6],"tags":[16,9,53,52,30,29,54],"class_list":["post-60","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-database","tag-java","tag-jpa","tag-mongodb","tag-nosql","tag-spring","tag-springboot"],"_links":{"self":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":18,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":969,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/posts\/60\/revisions\/969"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/media\/951"}],"wp:attachment":[{"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mutesoft.com\/spaces\/software\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}