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

GenerationType.IDENTITY problem on Hibernate for primary field

I'm facing a problem on auto generate id number for primary field using Hibernate. It does not generate as it should be, it jumps to 32 or 33.

After browsing through community, i found someone who face the same problem and there's a suggestion to use @GeneratedValue(strategy = GenerationType.TABLE).

https://teamtreehouse.com/community/not-sure-what-i-did-wrong-but-the-the-id-of-my-contacts-does-not-generate-the-way-it-should-it-goes-like-1-336597

But this forum said, TABLE is not recommended. I'm not really understand why though.

http://stackoverflow.com/questions/10041938/how-to-choose-the-id-generation-strategy-when-using-hibernate

Any idea how to solve this?

Craig Dennis , Chris Ramacciotti

Thanks

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Check my answer on StackOverflow

TL;DR

When you DON"T close SessionFactory properly and exiting application, then your database is eating out your IDs

add

sessionFactory.close()

at the end of the Main class