Hibernate Primary Key Generator Annotation

Primary Key Generation Using Table Generator. At this example, you would be seeing a database table for generating a primary keys. Anatomy of @TableGenerator. The @TableGenerator annotation defines a primary key generator that may be referenced by name when a generator element is specified for the @GeneratedValue. The JPA specification supports 4 different primary key generation strategies that generate the primary key values programmatically or use database features, like auto-incremented columns or sequences. The only thing you have to do is to add the @GeneratedValue annotation to your primary key attribute and choose a generation strategy.

  1. Spring Hibernate Annotations
  2. Hibernate Primary Key Generator Annotations
  3. Hibernate Annotation Example
Details
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

Apr 19, 2013  Hibernate Tutorial Part 7 - Primary key Auto - Generation in Hibernate ( Hands on). I would talk on how to generate the value of primary key automatically for a.

Example 7-8 shows how to use the @GeneratedValue annotation to specify a primary key value generator based on a primary key identity column (autonumber column). When a new instance of Address is persisted, the database assigns a value to the identity column. 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. Jun 23, 2009  Re: JPA, Hibernate: Custom primary key generator, before persist 843859 Jun 22, 2009 2:34 PM ( in response to Tolls ) So, you want to generate the barcode on the document, but ensure that the code is unique (ie using the database to keep track of the id possibly).

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

Spring Hibernate Annotations

If you use hibernate.cfg.xml file, specify connection properties for Oracle database as follows:Remember that the Hibernate Primary Key Generator Annotationhibernate.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 src/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:

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:


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.

The <generator> class is a sub-element of id. It is used to generate the unique identifier for the objects of persistent class. There are many generator classes defined in the Hibernate Framework.


All the generator classes implements the org.hibernate.id.IdentifierGenerator interface. The application programmer may create one's own generator classes by implementing the IdentifierGenerator interface. Hibernate framework provides many built-in generator classes:

  1. assigned
  2. increment
  3. sequence
  4. hilo
  5. native
  6. identity
  7. seqhilo
  8. uuid
  9. guid
  10. select
  11. foreign
  12. sequence-identity

1) assigned

It is the default generator strategy if there is no <generator> element . In this case, application assigns the id. For example:

2) increment

It generates the unique id only if no other process is inserting data into this table. It generates short, int or long type identifier. If a table contains an identifier then the application considers its maximum value else the application consider that the first generated identifier is 1. For each attribute value, the hibernate increment the identifier by 1. Syntax:

3) sequence

It uses the sequence of the database. if there is no sequence defined, it creates a sequence automatically e.g. in case of Oracle database, it creates a sequence named HIBERNATE_SEQUENCE. In case of Oracle, DB2, SAP DB, Postgre SQL or McKoi, it uses sequence but it uses generator in interbase. Syntax:

For defining your own sequence, use the param subelement of generator.

4) hilo

Primary

It uses high and low algorithm to generate the id of type short, int and long. Syntax:

5) native

It uses identity, sequence or hilo depending on the database vendor. Syntax:

6) identity

It is used in Sybase, My SQL, MS SQL Server, DB2 and HypersonicSQL to support the id column. The returned id is of type short, int or long. It is responsibility of database to generate unique identifier.

7) seqhilo

It uses high and low algorithm on the specified sequence name. The returned id is of type short, int or long.

8) uuid

It uses 128-bit UUID algorithm to generate the id. The returned id is of type String, unique within a network (because IP is used). The UUID is represented in hexadecimal digits, 32 in length.

Hibernate Primary Key Generator Annotations

9) guid

It uses GUID generated by database of type string. It works on MS SQL Server and MySQL.

10) select

It uses the primary key returned by the database trigger.

11) foreign

It uses the id of another associated object, mostly used with <one-to-one> association.

12) sequence-identity

Hibernate Annotation Example

It uses a special sequence generation strategy. It is supported in Oracle 10g drivers only.
Next TopicDialects In Hibernate