• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Thoughts on Java

  • Tutorials ▼
    • Hibernate & JPA Tutorials
    • Hibernate Tips
    • Tutorials on YouTube
  • Books & Courses ▼
    • Hibernate Tips Book
    • Online Training
    • Open Workshops
    • On-Site Training
  • Consulting ▼
    • Consulting Call
    • Project Coaching
  • About ▼
    • About Thoughts on Java
    • Support Thoughts on Java
    • Resources
  • Member Library
You are here: Home / Hibernate / Hibernate Tips / Hibernate Tips: How to use a custom database sequence
Do you live in Europe?
Then I hope to see you at one of my in-person workshops in December 2019. You will learn to use Hibernate advanced features and to improve the performance of your persistence layer.

Hibernate Tips: How to use a custom database sequence

By Thorben Janssen 2 Comments

  • tweet 
  • share 
  • share 
  • share 
  • share 
  • email 

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 you like me to answer, please leave a comment below.

Follow me on YouTube to not miss any new videos.

 

Question:

Hibernate uses its default database sequence to generate primary key values. How can I use my own sequence?

 

Solution:

The JPA specification provides a set of annotations to define the primary key generation strategy.

First of all, you have to annotate the primary key attribute with the @GeneratedValue annotation and set GenerationType.SEQUENCE as the strategy. This tells Hibernate to use a database sequence to generate the primary key value. If you don’t provide any additional information, Hibernate will use its default sequence.

You can configure the name and schema of the database sequence with a @SequenceGenerator annotation like the one you can see in the following code snippet.

   

If you now persist a new Author entity, Hibernate will use the configured database sequence “author_seq”. You can see that in the log file, if you activate the logging for SQL statements.  

Get this Hibernate Tip as a printable PDF!

Join the free Thoughts on Java Library to get access to lots of member-only content, like a printable PDF for this post, lots of cheat sheets and 2 ebooks about Hibernate.
Join Now!

Already a member? Login here.

 

Learn more:

Custom database sequences are only 1 out of 4 options to generate primary key values. I get into more details about the different strategies in How to generate primary keys with JPA and Hibernate.

 

  • tweet 
  • share 
  • share 
  • share 
  • share 
  • email 

Related posts:

  1. Hibernate Tips: Use an auto-incremented column as primary key
  2. Hibernate Tips: How to override the primary key generation strategy
  3. Hibernate Tips: How to model an association that doesn’t reference primary key columns
  4. Hibernate Tips: How to Share the Primary Key in a One-to-One Association
Become a Thoughts on Java Supporter to claim your member perks and to help me write more articles like this.

Filed Under: Hibernate Tips Tagged With: Mapping, Primary Key

Improve Your Hibernate Skills At An In-Person Workshop

Implement Your Persistence Layer with Ease

Learn More About Hibernate

Need Some Help with Your Project?

Reader Interactions

Comments

  1. Ashu Bhati says

    August 6, 2019 at 9:42 am

    am having problem if a am using generation type- Sequence and giving allocation size and Im size but it take default oracle sequence and giving an error how can solve this …please guide

    Reply
    • Thorben Janssen says

      August 6, 2019 at 1:03 pm

      Sounds like something is wrong with your mapping. Please share the code.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Join over 10.000 developers
in the
Thoughts on Java Library

Ebooks Sidebar Get free access to ebooks, cheat sheets and training videos.
Join Now!
Don't like ads?
Become a Thoughts on Java Supporter.
Special Launch Price

Let’s Connect


Thorben Janssen
Independent consultant, trainer and author
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter
  • xing
  • YouTube

Speaking at

2nd-3rd December 2019
Düsseldorf (Germany):
Advanced Hibernate Workshop (2-day Workshop - English)

4th-6th December 2019
Düsseldorf (Germany):
Hibernate Performance Tuning Workshop (3-day Workshop - English)

Looking for an on-site training?

Featured Post

14 YouTube Channels You Should Follow in 2019

Getting Started With Hibernate

Entities or DTOs – When should you use which projection?

Ultimate Guide – Association Mappings with JPA and Hibernate

Ultimate Guide to JPQL Queries with JPA and Hibernate

Recent Posts

  • Don’t expose your JPA entities in your REST API
  • Hibernate Tip: How to control cache invalidation for native queries
  • Hibernate Tip: How to customize the association mappings using a composite key
  • Hibernate Tip: How to integrate an external query builder
  • Hibernate Tip: Best Way To Work with Scalar Projections
  • 6 Hibernate Mappings You Should Avoid for High-Performance Applications
  • Hibernate Tip: Best Way To Work with Scalar Projections
  • Using the Optimal Query Approach and Projection for JPA and Hibernate
  • Implementing the Outbox Pattern with CDC using Debezium
  • Hibernate Tip: Difference between @JoinColumn and @PrimaryKeyJoinColumn
Don't like ads?
Become a Thoughts on Java Supporter.

Copyright © 2019 Thoughts on Java

  • Impressum
  • Disclaimer
  • Privacy Policy