Hibernate ORM with Oracle 23c Free

Rashmin Mudunkotuwa
4 min readJul 26, 2023
Photo by Jacqueline Godany on Unsplash

Recently Oracle released the newest version of their state of the art enterprise Database solution. The special thing about this edition was that it was completely FREE for the developers to use unlike the previous versions except the feature-capped OracleXE versions.

If you are using Java as your programming language there are mainly two popular ways to connect to a database like Oracle. First one is using the JDBC library which is provided by Java in java.sql and javax.sql packages. And the second one is using a Object Relational Mapping (ORM) specification like JPA. There are various implementations of JPA, Hibernate JPA being the most popular one.

In this quick article lets see how to connect a simple Java project with Oracle 23c using Hibernate. (As for the database, I would be using a dockerized version of Oracle 23c Free which could be found in here)

First step is to create a simple maven project using the quickstart Maven archetype.

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4

This will start a terminal wizard which would ask for group-id, artifact-id, version and package for which you could provide desired values.

For example.

groupId: com.rashm1n
artifactId: hibernatetest
version: 1.0
package: com.rashm1n.hibernatetest

After the project is generated, open it with your preferred IDE.

First thing we have to do is modify the pom.xml file to include the needed bare minimum dependencies. For Hibernate JPA to work properly, we would need two. First being the org.hibernate.orm.hibernate-core dependency and the second being the JDBC driver of your preferred database, in our case Oracle JDBC Driver, the OJDBC.

<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.2.0.0</version>
</dependency>

<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
</dependency>

(I am using Java 17 in this instance and the appropriate OJDBC version is 11, which would be suitable for Java version from 9 and upwards. If you are using Java 8, you would need to get OJDBC8)

the final pom.xml file would look something similar to this.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.rashm1n.hibernate-test</groupId>
<artifactId>hibernate-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>hibernatetest</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.2.0.0</version>
</dependency>

<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
</dependency>
</dependencies>
</project>

Now that we have obtained the necessary jars to our class-path via maven, we need a way to tell Hibernate ORM how to find out database. Various method exists for this and the approach I’m taking is creating a hibernate.cfg.xml file which would include our database details.

You would need to create a resources folder in your src/main directory and create an empty file hibernate.cfg.xml in it. Your project structure would look something like this.

project-structure

In the hibernate.cfg.xml file we would specify our connection details and other required parameters.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection properties - Driver, URL, user, password -->
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521/FREEPDB1</property>
<property name="hibernate.connection.username">DB_USER</property>
<property name="hibernate.connection.password">DB_PASSWORD</property>

<property name="hibernate.connection.pool_size">5</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>

<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

As you can see above I have provided the driver_class, url, username and password to the relevant hibernate properties. Here you would need to replace the DB_USER and DB_PASSWORD with your own credentials.

After this you are all set to create your first entities and persist them.

Lets create a simple entity in a separate package entity as below.

package com.rashm1n.hibernatetest.Entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

private String author;
private int pages;

public Book(String author, int pages) {
this.author = author;
this.page = pages;
}

public Book() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getAuthor() {
return isbn;
}

public void setAuthor(String author) {
this.author = author;
}

public int getPages() {
return pages;
}

public void setPages(int pages) {
this.pages = pages;
}
}

Now go to the App.java in your root directory and enter the below code to obtain a SessionFactory from our configuration file and use that to create a Session object which would give us access to persist objects.

package com.rashm1n.hibernatetest;

import com.rashm1n.hibernatetest.Entity.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class App
{
public static void main( String[] args )
{
Session session = getSessionFromCfg();
Transaction transaction = session.beginTransaction();
Book b = new Book("JRRT","LOTR");
session.persist(b);
transaction.commit();
session.close()
}

public static Session getSessionFromCfg() {
Configuration config = new Configuration();
config.configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
return session;
}
}

This method will persist the Book object we created into the Oracle 23c database successfully.

This has been a very brief article about how to connect to the Oracle 23c database by using Hibernate ORM. I hope you enjoyed the read and found this useful. Cheers !

Want to Connect ?

- Medium
- LinkedIn
- Twitter(X)
- Threads

Find me everywhere @rashm1n.
Resources

- https://hibernate.org/orm/
- https://docs.oracle.com/en/database/oracle/oracle-database/23/index.html

--

--

Rashmin Mudunkotuwa

Software Engineer | Interested in Cloud Computing, Microservices, API Development, and Software as a whole.