Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Hibernate Basics Persisting Data with Hibernate Updating and Deleting Entities

Why aren't the IDs generated by Hybernate consecutive?

Pretty self explaining! :)

I am seeing that the IDs that Hybernate generates are "1, 33, 65,..."

What are those "32 missing IDs"?

2 Answers

Boban Talevski
Boban Talevski
24,793 Points

As was noted in some other post regarding a previous video in this course, this can be fixed by simply closing your sessionFactory object at the end of the main method in Application.java.

I think the logic behind it was that it "grabs" the next 32 ids when opened, and unless properly closed, it doesn't "return" those unused ids, and they are lost forever. Which is the reason, why there's a gap of 32 on each subsequent application run.

So just add this right before the closing tag of the main method and it should be fixed:

sessionFactory.close();
angel juarez
angel juarez
15,886 Points

thanks, I had the same issue and now it's fixed!

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

I'd have a look at the documentation for ID generation in Hibernate. My guess would be that the default generator is trying to ensure uniqueness there.