Skip to main content
This tutorial shows you how build a simple Java application with CockroachDB and the Hibernate ORM.
We recommend using Java versions 8+ with CockroachDB.
For another use of Hibernate with CockroachDB, see our examples-orms repository.

Step 1. Start CockroachDB

Choose your installation method

You can create a CockroachDB Basic cluster using either the CockroachDB Cloud Console, a web-based graphical user interface (GUI) tool, or ccloud, a command-line interface (CLI) tool.

Create a free cluster

Organizations without billing information on file can only create one CockroachDB Basic cluster.
  1. If you haven’t already, sign up for a CockroachDB Cloud account.
  2. Log in to your CockroachDB Cloud account.
  3. On the Clusters page, click Create cluster.
  4. On the Select a plan page, select Basic.
  5. On the Cloud & Regions page, select a cloud provider (GCP or AWS) in the Cloud provider section.
  6. In the Regions section, select a region for the cluster. Refer to for the regions where CockroachDB Basic clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
  7. Click Next: Capacity.
  8. On the Capacity page, select Start for free. Click Next: Finalize.
  9. On the Finalize page, click Create cluster. Your cluster will be created in a few seconds and the Create SQL user dialog will display.

Create a SQL user

The Create SQL user dialog allows you to create a new SQL user and password.
  1. Enter a username in the SQL user field or use the one provided by default.
  2. Click Generate & save password.
  3. Copy the generated password and save it in a secure location.
  4. Click Next. Currently, all new SQL users are created with admin privileges. For more information and to change the default settings, see .

Get the connection information

The Connect to cluster dialog shows information about how to connect to your cluster.
  1. Select Parameters only from the Select option dropdown.
  2. Copy the connection information for each parameter displayed and save it in a secure location.

Step 2. Get the sample code

Clone the example-app-java-hibernate repo to your machine:
The version of the CockroachDB Hibernate dialect in hibernate.cfg.xml corresponds to a version of CockroachDB. For more information, see .

Step 3. Run the code

The sample code in this tutorial (Sample.java) uses Hibernate to map Java methods to SQL operations. The code performs the following operations, which roughly correspond to method calls in the Sample class:
  1. Creates an accounts table as specified by the Account mapping class.
  2. Inserts rows into the table with the addAccounts() method.
  3. Transfers money from one account to another with the transferFunds() method.
  4. Prints out account balances before and after the transfer with the getAccountBalance() method.
In addition, the code shows a pattern for automatically handling by wrapping transactions in a higher-order function named runTransaction(). It also includes a method for testing the retry handling logic (Sample.forceRetryLogic()), which will be run if you set the FORCE_RETRY variable to true. It does all of the above using the practices we recommend for using Hibernate (and the underlying JDBC connection) with CockroachDB, which are listed in the Recommended Practices section below. The contents of Sample.java:

Update the connection configuration

Open src/main/resources/hibernate.cfg.xml, and set the hibernate.connection.url, hibernate.connection.username, and hibernate.connection.password properties, using the connection information that you obtained from the Cloud Console:

Run the code

Compile and run the code using gradlew, which will also download the dependencies:
Toward the end of the output, you should see:

Generate PKCS8 keys for client authentication

You can pass the to to generate a key in PKCS#8 format, which is the standard key encoding format in Java. For example, if you have the user max:
The generated PKCS8 key will be named client.max.key.pk8.
CockroachDB Cloud does not yet support certificate-based user authentication.

Use IMPORT INTO to read in large data sets

If you are trying to get a large data set into CockroachDB all at once (a bulk import), avoid writing client-side code altogether and use the statement instead. It is much faster and more efficient than making a series of and . It bypasses the altogether and writes directly to the of the database. For more information about importing data from PostgreSQL, see . For more information about importing data from MySQL, see .

Use reWriteBatchedInserts for increased speed

We strongly recommend setting reWriteBatchedInserts=true; we have seen 2-3x performance improvements with it enabled. From the JDBC connection parameters documentation:
This will change batch inserts from insert into foo (col1, col2, col3) values (1,2,3) into insert into foo (col1, col2, col3) values (1,2,3), (4,5,6) this provides 2-3x performance improvement

Retrieve large data sets in chunks using cursors

CockroachDB now supports the PostgreSQL wire-protocol cursors for implicit transactions and explicit transactions executed to completion. This means the PGJDBC driver can use this protocol to stream queries with large result sets. This is much faster than . For instructions showing how to use cursors in your Java code, see Getting results based on a cursor from the PGJDBC documentation. Note that interleaved execution (partial execution of multiple statements within the same connection and transaction) is not supported when Statement.setFetchSize() is used.

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 the following pages: