Thursday, 12 March 2015

Hibernate Issue - Org.Hibernate.AnnotationException: Unknown Id.Generator

Problem

Runing the following Hibernate’s annotation sequence generator with PostgreSQL database.
        @Id 
 @Column(name="user_id", nullable=false) 
 @GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="account_user_id_seq")
 private Integer userId;
Hits the following Unknown Id.generator exception.
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: account_user_id_seq
 at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:413)
 at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1795)
 at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1229)
 at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:733)
The sequence “account_user_id_seq” is created in PostgreSQL database, what caused the above exception?

Solution

When declaring the Hibernate’s annotation strategy to use “Sequences” as Id generator, try specify the @SequenceGenerator as well, as following
        @Id 
 @Column(name="user_id", nullable=false) 
 @SequenceGenerator(name="my_seq", sequenceName="account_user_id_seq")
 @GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="my_seq")
 private Integer userId;

No comments:

Post a Comment