In this article, we are discussing the hibernate exception org.hibernate.AnnotationException : Unknown Id.generator. When you work on hibernate project with '@GeneratedValue' annotation then there might be some chances of this exception occuring.
Below is a small code snippet with hibernate annotation that demonstrates this exception.
The @GeneratedValue annotation allows you to define a generation strategy for the primary key values. In the above code snippet, I wanted to use a database sequence and provide 'employeeSequence' as the name of the generator. A lot of developers now expect that 'employeeSequence' will be the name of the database sequence which Hibernate shall use. That's not the case. It's the name of the @SequenceGenerator which you can use to provide more information about the database sequence Hibernate shall use. But the definition of the @SequenceGenerator is missing, and Hibernate, therefore, throws the AnnotationException. To fix it, you have to add a @SequenceGenerator annotation as I did in the following code snippet.
When you use @GeneratedValue annotation, it should be accompanied by @SequenceGenerator annotation, which allows you to provide more information about the database sequence and how Hibernate shall use it. In this code snippet, I have set the name of the sequence, which is "emp_seq" and 1000 as its initial value. You can also specify the database schema to which the sequence belongs and the allocation size which Hibernate can use for performance optimizations.
Looking for any Software or Tutorial?
Don't Worry, we will find it for you