Skip to content

Latest commit

 

History

History
105 lines (75 loc) · 4.31 KB

build-a-java-app-with-cockroachdb-hibernate.md

File metadata and controls

105 lines (75 loc) · 4.31 KB
title summary toc twitter
Build a Java App with CockroachDB
Learn how to use CockroachDB from a simple Java application with the Hibernate ORM.
false
true

This tutorial shows you how build a simple Java application with CockroachDB using a PostgreSQL-compatible driver or ORM. We've tested and can recommend the Java jdbc driver and the Hibernate ORM, so those are featured here.

{{site.data.alerts.callout_success}}For a more realistic use of Hibernate with CockroachDB, see our examples-orms repository.{{site.data.alerts.end}}

Before You Begin

Make sure you have already installed CockroachDB.

Step 1. Install the Gradle build tool

This tutorial uses the Gradle build tool to get all dependencies for your application, including the Hibernate ORM. To install Gradle, run the following command:

# On Mac:
$ brew install gradle

# On Ubuntu Linux:
$ apt-get install gradle

For other ways to install Gradle, see the official documentation.

{% include app/common-steps.md %}

Step 5. Run the Java code

Downlod and extract this tarball, which includes three files that work together:

File Description
hibernate.cfg.xml This file specifies how to connect to the database and that the database schema will be deleted and recreated each time the app is run. It must be in the src/main/resources directory.
Sample.java This file uses the Hibernate ORM to map Java-specific objects to SQL operations. It must be in the src/main/java/com/cockroachlabs/ directory.
build.gradle This is the file you run to execute your app.

For more insight into this sample application, review the Sample.java file, which uses the Hibernate ORM to map Java-specific objects to SQL operations. Specifically, an accounts table gets created based on the Account class, session.save(new Account()) inserts rows into the table, and the CriteriaQuery<Account> query object defines the SQL query for selecting from the table so that balances can be printed.

{% include app/hibernate-basic-sample/Sample.java %}

Then in the hibernate-basic-sample directory, run the gradle file to fetch the dependencies in Sample.java (including Hibernate) and run the application:

$ gradle run

Toward the end of the output, you should see:

1 1000
2 250

To verify that the table and rows were created successfully, you can again use the built-in SQL client:

$ cockroach sql -e 'SHOW TABLES' --database=bank
+----------+
|  Table   |
+----------+
| accounts |
+----------+
(1 row)
$ cockroach sql -e 'SELECT id, balance FROM accounts' --database=bank
+----+---------+
| id | balance |
+----+---------+
|  1 |    1000 |
|  2 |     250 |
+----+---------+
(2 rows)

What's Next?

Read more about using the Hibernate ORM, or check out a more realistic implementation of Hibernate with CockroachDB in our examples-orms repository.

You might also be interested in using a local cluster to explore the following core CockroachDB features: