The @SequenceGenerator annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation.A sequence generator may be specified on the entity class or on the primary key field or property. Target: Type, Method and Field. If you don't specify any generator for your primary key attribute, hibernate will assume you use the assign strategy. The assign strategy tells to hibernate that you will set id yourself, and so hibernate won't help you to define the id value. (hibernate will just throw an exception if you try to persist an entity with a null/already existing id). Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question for a future Hibernate Tip, please leave a comment below. Question: You explained that the GenerationType.SEQUENCE is the most efficient primary key generation strategy and that MySQL doesn’t support it.

Written by Nam Ha Minh
Last Updated on 04 October 2019 Print Email
In this Hibernate tutorial, I will guide you how to configure Hibernate framework to work with Oracle database. The code examples below are tested with Hibernate 5 and Oracle Express database 18c. Here are the steps:

1. Use JDBC driver for Oracle database

A JDBC driver for Oracle must be present in your project’s classpath. Click here to download Oracle Database JDBC driver. Choose the version according to your Oracle database installation (you must have an account in Oracle website to download. Sign up is free).Extract the downloaded archive file and add the ojdbc8.jar to the project’s classpath, e.g. in Eclipse IDE:In case you use Maven, add the following dependency into the pom.xml file:Due to Oracle’s license restriction, you can’t download the driver directly from Maven’s repository, so you have to use dependency like that.

2. Specify database connection properties

If you use hibernate.cfg.xml file, specify connection properties for Oracle database as follows:Remember that the hibernate.cfg.xml file must be put in the src/main/resources folder of your project.In case you use JPA/Hibernate, specify database connection properties in the persistence.xml file like this:Note that the persistence.xml file must be in the Primary key adalahsrc/main/resources/META-INF folder.

3. Create Sequence in Oracle database

Since Oracle doesn’t have auto-increment feature for primary key, you must use sequence instead. Use the following annotation for mapping the primary key field in a model class:For example, the Customer class that maps to the Customers table in the database:In this case, Hibernate will look for the sequence named HIBERNATE_SEQUENCE, so you need to create such a sequence in the database, using the following statement:If you want to use another sequence name, use the @SequenceGeneratorannotation as follows:Then create the sequence in the database accordingly:You can use SQLPlus or SQL Developer tool to create the sequence.

4. Hibernate Example Program

For your reference, the following example program uses Hibernate to persist a Customerobject to the Oracle database:In case you want to use JPA with Hibernate, here’s another sample program:As you can see, code remains the same for different databases. So to use Hibernate with Oracle database, you need to use proper JDBC driver, connection properties and create sequence.

Some Notes for Hibernate and Oracle database:

Hibernate Primary Key Generator Annotations

If somehow Hibernate inserts negative value for the ID column, you must specify allocation size for the sequence like this:The value of the allocation size attribute be as same as the value of the “increment by” field of the sequence in Oracle database.If the database table uses trigger to automatically insert values for the ID column, and the Hibernate’s SequenceGeneratordoesn’t work somehow, try this solution:You can also watch the video version here:

Other Hibernate Tutorials:

Hibernate Annotations Tutorial

About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook.
Coments are closed
Scroll to top