{"id":63217,"date":"2022-03-28T09:01:09","date_gmt":"2022-03-28T09:01:09","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=63217"},"modified":"2022-03-28T09:01:09","modified_gmt":"2022-03-28T09:01:09","slug":"simplify-java-persistence-implementation-with-kotlin-on-quarkus","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=63217","title":{"rendered":"Simplify Java persistence implementation with Kotlin on Quarkus"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">Simplify Java persistence implementation with Kotlin on Quarkus<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/daniel-oh\" class=\"username\">Daniel Oh<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Mon, 03\/28\/2022 &#8211; 03:00<\/span><\/p>\n<div data-drupal-selector=\"rate-node-69776\" class=\"rate-widget-thumbs-up\">\n<div class=\"rate-thumbs-up-btn-up rate-thumbs-up-btn-up vote-pending\">Up<\/div>\n<div class=\"rate-score\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to like.<\/div>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>For decades, developers have struggled with optimizing persistence layer implementation in terms of storing business data, retrieving relevant data quickly, and\u2014most importantly\u2014 simplifying data transaction logic regardless of programming languages.<\/p>\n<p>Fortunately, this challenge triggered the invention of Java ecosystems in which developers can implement the Java Persistence API (JPA). For instance, Hibernate Object Relational Mapper (ORM) with Panache is the standard framework for JPA implementation in the Java ecosystem.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">Explore the open source cloud<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/do092-developing-cloud-native-applications-microservices-architectures?intcmp=7013a000002gp8aAAA\">Free online course: Developing cloud-native applications with microservices arc\u2026<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/engage\/managed-cloud-services-s-202202210207?intcmp=7013a000002gp8aAAA\">eBook: Modernize your IT with managed cloud services<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/cloud.redhat.com\/products\/dedicated\/?intcmp=7013a000002gp8aAAA\">Try for 60 days: Red Hat OpenShift Dedicated<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/topics\/containers\/what-is-kubernetes?intcmp=7013a000002gp8aAAA\">What is Kubernetes?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/topics\/edge-computing?intcmp=7013a000002gp8aAAA\">Understanding edge computing<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/architect\/?intcmp=7013a000002gp8aAAA\">Latest articles for IT architects<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p><a href=\"https:\/\/opensource.com\/article\/20\/6\/java-kotlin\/\">Kotlin<\/a> is a programming language designed to run business applications with multiple programming languages on top of Java Virtual Machine (JVM) for the Java persistence implementation. But there&#8217;s still the hurdle for Java developers to catch up on the new syntax and JPA APIs of Kotlin.<\/p>\n<p>This article will explain how Quarkus makes it easier for developers to implement Kotlin applications through the Quarkus Hibernate ORM Panache Kotlin extension.<\/p>\n<h2>Create a new Hibernate ORM Kotlin project using Quarkus CLI<\/h2>\n<p>First, create a new <a href=\"https:\/\/opensource.com\/article\/22\/3\/maven-manage-java-dependencies\">Maven<\/a> project using the Quarkus command-line tool (CLI). The following command will add Quarkus extensions to enable <code>Hibernate ORM Panache Kotlin<\/code> and <code>PostgreSQL JDBC<\/code> extensions:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ quarkus create app hibernate-kotlin-example <br>\n\u00a0<span class=\"re5\">-x<\/span> jdbc-postgresql, hibernate-orm-panache-kotlin<\/div><\/div><\/pre>\n<p>The output should look like this:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">...<br><span class=\"sy0\">&lt;<\/span><span class=\"br0\">[<\/span>SUCCESS<span class=\"br0\">]<\/span> &#x2705; quarkus project has been successfully generated in:<br>\n--<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">\/<\/span>Users<span class=\"sy0\">\/<\/span>danieloh<span class=\"sy0\">\/<\/span>Downloads<span class=\"sy0\">\/<\/span>demo<span class=\"sy0\">\/<\/span>hibernate-kotlin-example<br>\n...<\/div><\/div><\/pre>\n<h2>Create a new entity and repository<\/h2>\n<p>Hibernate ORM with Panache enables developers to handle entities with active record patterns with the following benefits:<\/p>\n<ul>\n<li>Auto-generation of IDs<\/li>\n<li>No need for getters\/setters<\/li>\n<li>Super useful static methods for access such as <code>listAll()<\/code> and <code>find()<\/code><\/li>\n<li>No need for custom queries for basic operations (e.g. <code>Person.find<\/code> (&#8220;name&#8221;, &#8220;daniel&#8221;))<\/li>\n<\/ul>\n<p>Kotlin doesn&#8217;t support the Hibernate ORM with Panache in the same way Java does. Instead, Quarkus allows developers to bring these capabilities into Kotlin using the <code>companion<\/code> object, as illustrated below:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\"><span class=\"sy0\">@<\/span>Entity<span class=\"br0\">(<\/span>name = <span class=\"st0\">\"Person\"<\/span><span class=\"br0\">)<\/span><br>\nclass Person : PanacheEntity<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span><br>\nlateinit var name: String<br><span class=\"br0\">}<\/span><\/div><\/div><\/pre>\n<p>Here is a simple example of how developers can implement\u00a0<code>PanacheRepository<\/code> to access the entity:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\"><span class=\"sy0\">@<\/span>ApplicationScoped<br>\nclass PersonRepository: PanacheRepository<span class=\"sy0\">&lt;<\/span>Person<span class=\"sy0\">&gt;<\/span> <span class=\"br0\">{<\/span><br>\nfun findByName<span class=\"br0\">(<\/span>name: String<span class=\"br0\">)<\/span> = <span class=\"kw2\">find<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"name\"<\/span>, name<span class=\"br0\">)<\/span>.firstResult<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><br><span class=\"br0\">}<\/span><\/div><\/div><\/pre>\n<p>Super simple! Now I&#8217;ll show you how to implement resources to access data by RESTful APIs.<\/p>\n<h2>Create a resource to handle RESTful APIs<\/h2>\n<p>Quarkus fully supports context and dependency injection (CDI), which allows developers to inject <code>PersonRepository<\/code> to access the data (e.g., <code>findByName(name)<\/code>) in the resources. For example:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\"><span class=\"sy0\">@<\/span>Inject<br>\nlateinit var personRepository: PersonRepository<br><br><span class=\"sy0\">@<\/span>GET<br><span class=\"sy0\">@<\/span>Produces<span class=\"br0\">(<\/span>MediaType.TEXT_PLAIN<span class=\"br0\">)<\/span><br><span class=\"sy0\">@<\/span>Path<span class=\"br0\">(<\/span><span class=\"st0\">\"\/{name}\"<\/span><span class=\"br0\">)<\/span><br>\nfun greeting<span class=\"br0\">(<\/span><span class=\"sy0\">@<\/span>PathParam<span class=\"br0\">(<\/span><span class=\"st0\">\"name\"<\/span><span class=\"br0\">)<\/span> name: String<span class=\"br0\">)<\/span>: String <span class=\"br0\">{<\/span><br><span class=\"kw3\">return<\/span> <span class=\"st0\">\"Hello <span class=\"es3\">${personRepository.findByName(name)?.name}<\/span>\"<\/span><br><span class=\"br0\">}<\/span><\/div><\/div><\/pre>\n<h2>Run and test the application<\/h2>\n<p>As always, run your Quarkus application using Developer Mode:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw3\">cd<\/span> hibernate-kotlin-example<br>\n$ quarkus dev<\/div><\/div><\/pre>\n<p>The output should look like this:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">...<br>\nINFO <span class=\"br0\">[<\/span>io.quarkus<span class=\"br0\">]<\/span> <span class=\"br0\">(<\/span>Quarkus Main Thread<span class=\"br0\">)<\/span> Profile dev activated. <br>\nLive Coding activated.<br>\nINFO <span class=\"br0\">[<\/span>io.quarkus<span class=\"br0\">]<\/span> <span class=\"br0\">(<\/span>Quarkus Main Thread<span class=\"br0\">)<\/span> <br>\nInstalled features: <span class=\"br0\">[<\/span>agroal, cdi, hibernate-orm, <br>\nhibernate-orm-panache-kotlin, jdbc-postgresql, <br>\nkotlin, narayana-jta, resteasy, smallrye-context-propagation, vertx<span class=\"br0\">]<\/span><br><br><span class=\"re5\">--<\/span><br>\nTests paused<br>\nPress <span class=\"br0\">[<\/span>r<span class=\"br0\">]<\/span> to resume testing, <span class=\"br0\">[<\/span>o<span class=\"br0\">]<\/span> Toggle <span class=\"kw3\">test<\/span> output, <br><span class=\"br0\">[<\/span>:<span class=\"br0\">]<\/span> <span class=\"kw1\">for<\/span> the terminal, <span class=\"br0\">[<\/span>h<span class=\"br0\">]<\/span> <span class=\"kw1\">for<\/span> <span class=\"kw2\">more<\/span> options<span class=\"sy0\">&gt;<\/span><\/div><\/div><\/pre>\n<p><a href=\"https:\/\/opensource.com\/article\/21\/7\/developer-productivity-linux\">Quarkus Dev services<\/a> stand up a relevant database container automatically when you run a container runtime (e.g., Docker or Podman) and add a database extension. In this example, I already added the <code>jdbc-postgresql<\/code> extension, so a PostgreSQL container will be running automatically when the Quarkus Dev mode begins. Find the solution in my <a href=\"https:\/\/github.com\/danieloh30\/hibernate-kotlin-example\">GitHub repository<\/a>.<\/p>\n<p>Access the RESTful API (<code>\/hello<\/code>) to retrieve the data by the <code>name<\/code> parameter. Execute the following <code>curl<\/code> command line in your local terminal:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"sy0\">&amp;<\/span> curl localhost:<span class=\"nu0\">8080<\/span><span class=\"sy0\">\/<\/span>hello<span class=\"sy0\">\/<\/span>Daniel<\/code><\/span><\/pre>\n<p>The output should look like this:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\">Hello Daniel<\/code><\/span><\/pre>\n<h2>Conclusion<\/h2>\n<p>This is a basic explanation of how Quarkus enables developers to simplify JPA implementation using Kotlin programming APIs for reactive Java applications. Developers can also have better developer experiences, such as dev services and live coding, while they keep developing with Kotlin programming in Quarkus. For more information about Quarkus, visit the <a href=\"https:\/\/quarkus.io\/\">Quarkus web page.<\/a><\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>This article demonstrates how Quarkus enables developers to simplify JPA implementation using Kotlin programming APIs for reactive Java applications.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/03\/lenovo-thinkpad-laptop-concentration-focus-windows-office-1.png\" width=\"799\" height=\"447\" alt=\"Business woman on laptop sitting in front of window\" title=\"Woman using laptop concentrating\" loading=\"lazy\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Image by Mapbox Uncharted ERG,\u00a0<a href=\"https:\/\/creativecommons.org\/licenses\/by\/3.0\/us\/\" rel=\"noreferrer nofollow noopener\" target=\"_blank\">CC-BY 3.0 US<\/a><\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/java\" hreflang=\"en\">Java<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/kubernetes\" hreflang=\"en\">Kubernetes<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/cloud\" hreflang=\"en\">Cloud<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/03\/cc-by-sa--13.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simplify Java persistence implementation with Kotlin on Quarkus Daniel Oh Mon, 03\/28\/2022 &#8211; 03:00 Up Register or Login to like. For decades, developers have struggled with optimizing persistence layer implementation in terms of storing business data, retrieving relevant data quickly, and\u2014most importantly\u2014 simplifying data transaction logic regardless of programming languages. Fortunately, this challenge triggered the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":63218,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-63217","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/63217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=63217"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/63217\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/63218"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=63217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=63217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=63217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}