• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

Simplify Java persistence implementation with Kotlin on Quarkus

Simplify Java persistence implementation with Kotlin on Quarkus: Simplify Java persistence implementation with Kotlin on Quarkus Daniel Oh Mon, 03/28/2022 – 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—most importantly— simplifying data transaction logic regardless of programming languages.

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.

Explore the open source cloud
Free online course: Developing cloud-native applications with microservices arc…
eBook: Modernize your IT with managed cloud services
Try for 60 days: Red Hat OpenShift Dedicated
What is Kubernetes?
Understanding edge computing
Latest articles for IT architects

Kotlin 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’s still the hurdle for Java developers to catch up on the new syntax and JPA APIs of Kotlin.

This article will explain how Quarkus makes it easier for developers to implement Kotlin applications through the Quarkus Hibernate ORM Panache Kotlin extension.

Create a new Hibernate ORM Kotlin project using Quarkus CLI

First, create a new Maven project using the Quarkus command-line tool (CLI). The following command will add Quarkus extensions to enable Hibernate ORM Panache Kotlin and PostgreSQL JDBC extensions:

 
$ quarkus create app hibernate-kotlin-example \
 -x jdbc-postgresql, hibernate-orm-panache-kotlin

The output should look like this:

 
...
<[SUCCESS] ? quarkus project has been successfully generated in:
--> /Users/danieloh/Downloads/demo/hibernate-kotlin-example
...

Create a new entity and repository

Hibernate ORM with Panache enables developers to handle entities with active record patterns with the following benefits:

  • Auto-generation of IDs
  • No need for getters/setters
  • Super useful static methods for access such as listAll() and find()
  • No need for custom queries for basic operations (e.g. Person.find (“name”, “daniel”))

Kotlin doesn’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 companion object, as illustrated below:

 
@Entity(name = "Person")
class Person : PanacheEntity() {
lateinit var name: String
}

Here is a simple example of how developers can implement PanacheRepository to access the entity:

 
@ApplicationScoped
class PersonRepository: PanacheRepository<Person> {
fun findByName(name: String) = find("name", name).firstResult()
}

Super simple! Now I’ll show you how to implement resources to access data by RESTful APIs.

Create a resource to handle RESTful APIs

Quarkus fully supports context and dependency injection (CDI), which allows developers to inject PersonRepository to access the data (e.g., findByName(name)) in the resources. For example:

 
@Inject
lateinit var personRepository: PersonRepository

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/{name}")
fun greeting(@PathParam("name") name: String): String {
return "Hello $ {personRepository.findByName(name)?.name}"
}

Run and test the application

As always, run your Quarkus application using Developer Mode:

 
$ cd hibernate-kotlin-example
$ quarkus dev

The output should look like this:

 
...
INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. \
Live Coding activated.
INFO [io.quarkus] (Quarkus Main Thread) \
Installed features: [agroal, cdi, hibernate-orm, \
hibernate-orm-panache-kotlin, jdbc-postgresql, \
kotlin, narayana-jta, resteasy, smallrye-context-propagation, vertx]

--
Tests paused
Press [r] to resume testing, [o] Toggle test output, \
[:] for the terminal, [h] for more options>

Quarkus Dev services 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 jdbc-postgresql extension, so a PostgreSQL container will be running automatically when the Quarkus Dev mode begins. Find the solution in my GitHub repository.

Access the RESTful API (/hello) to retrieve the data by the name parameter. Execute the following curl command line in your local terminal:

 & curl localhost:8080/hello/Daniel

The output should look like this:

 Hello Daniel

Conclusion

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 Quarkus web page.

This article demonstrates how Quarkus enables developers to simplify JPA implementation using Kotlin programming APIs for reactive Java applications.

Business woman on laptop sitting in front of window
Image by:

Image by Mapbox Uncharted ERG, CC-BY 3.0 US

Java
Kubernetes
Cloud

What to read next
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com Daniel Oh

Supporting Open Source.

Have you tried: Travelling to South Africa?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 28 March 2022 by Alan Category: SoftwareTag: Implementation, java, Kotlin, persistence, Quarkus, Simplify

Fighting Wild Fires with Cognitive Computing

Check out this South African City image: Fighting Wild Fires with Cognitive Computing Image by IBM …

Read moreFighting Wild Fires with Cognitive Computing

FREE Vegetarian Cooking Class

FREE Vegetarian Cooking Class – a Africa interest event taking place on 2019-01-13 15:30:00 in …

Read moreFREE Vegetarian Cooking Class

Little River Band

Little River Band – a Music interest event taking place on 2021-07-17 20:00:00 in United …

Read moreLittle River Band

About Alan

Previous Post:Nice Jamie-Lea’s 2019 Matric Dance photo
Next Post:Trending Now: Oscars

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top