org.hibernate.AnnotationException: Unknown Id generator Solution

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.

@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employeeSequence") @Column(name = "id", updatable = false, nullable = false) private Long id;

Exception Stacktrace:

Caused by: org.hibernate.AnnotationException: Unknown Id.generator: employeeSequence at org.hibernate.cfg.BinderHelper.makeIdGenerator( at org.hibernate.cfg.AnnotationBinder.bindId( at org.hibernate.cfg.AnnotationBinder.processElementAnnotations( at org.hibernate.cfg.AnnotationBinder.bindClass(

Description about 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.


@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employeeSequence") @SequenceGenerator(name = "employeeSequence", sequenceName = "emp_seq", initialValue = 1000) @Column(name = "id", updatable = false, nullable = false) private Long id;

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.

Solution Worked 0 UpvotesUpvote
Solution Didn't Worked 0 DownvotesDownvote