How to Get Started with Doma: A Comprehensive Guide

Nov 18, 2021 | Programming

Doma is a powerful database access framework for Java that simplifies data manipulation while ensuring type safety and compile-time verification. Whether you’re a seasoned developer or a curious beginner, this guide will help you navigate through the essentials of Doma and its features.

Prerequisites

  • Ensure you are using Java 17 or above. If you’re using Java 8, switch to Doma version 2.

Supported Databases

Doma supports a variety of databases, ensuring stability across multiple platforms. Here’s a quick glance:

  • H2 Database: 2.3.x – Stable
  • MySQL: v5.7 and v8.0.36 – Stable
  • Oracle Database: XE 21c – Stable
  • PostgreSQL: 12.20 – Stable
  • SQL Server: 2019 – Stable

Key Features of Doma

Doma boasts several compelling features:

  • Compile-time verification and code generation via annotation processing.
  • Type-safe Criteria API for query construction.
  • Support for Kotlin programming.
  • Two-way SQL templates for dynamic SQL queries.
  • No dependencies on other libraries, promoting a lightweight framework.

Example: Using Type-safe Criteria API

Imagine Doma as a magic calculator that helps you solve complex math problems (SQL queries) using simple, intuitive commands. The code snippet below showcases how you can leverage Doma’s Type-safe Criteria API:

javavar entityql = new Entityql(config);
var e = new Employee_();
var d = new Department_();
var employees = entityql
.from(e)
.innerJoin(d, on - on.eq(e.departmentId, d.departmentId))
.where(c - c.eq(d.departmentName, SALES))
.associate(e, d, (employee, department) -
employee.setDepartment(department);
department.getEmployeeList().add(employee);
)
.fetch();

In this analogy, you are instructing the magic calculator step-by-step: first, specify which numbers (employees) to include, then define the relationship (inner join) and filtering criteria (the department name). Finally, you establish connections between employees and their departments before retrieving the solution (the employee list).

Getting Started with Installation

Follow the steps below to install Doma in your project:

For Gradle Users

In your Java project, add the following lines to your build.gradle file:

plugins {
id 'org.domaframework.doma.compile' version '2.0.0'
}
dependencies {
implementation 'org.seasar.doma:doma-core:3.0.1'
annotationProcessor 'org.seasar.doma:doma-processor:3.0.1'
}

For Maven Users

For Java projects using Maven, include the following configuration:



org.seasar.doma
doma-core
${doma.version}





org.apache.maven.plugins
maven-compiler-plugin
3.8.1

17
17


org.seasar.doma
doma-processor
${doma.version}



-Adoma.resources.dir=${project.basedir}/src/main/resources




Troubleshooting

If you encounter any issues while using Doma, consider these troubleshooting steps:

  • Ensure your Java version is compatible; Doma 3 supports Java 17 and above.
  • Check for any syntax errors in your SQL templates.
  • If you have dependency issues, verify that the Doma versions align in your build configuration.
  • Refer to the official documentation to ensure you’re using the latest API features.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox