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;

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
9 Comment authors
VivekAshwini SharmaAaparnaAlexnmn Recent comment authors
newest oldest most voted
Aaparna
Guest
Aaparna

I tryied the similar way it didn’t work, But the below code did.

@Id

@Column(name = “ID”, unique = true, nullable = false)

@GeneratedValue(generator=”my_seq”)

@SequenceGenerator(name=”my_seq”, sequenceName=”E_ID”)

private Long docmId;

Ashwini Sharma
Guest
Ashwini Sharma

may u plz tell me how it works…

Vivek
Guest
Vivek

Hi Mkyong,
Please give some solution to my issue.

Thanks
Vivek

Vivek
Guest
Vivek

How I can autogenerate the String in hibernate?

Example:

@Id

@GenerateValue

private String cust_code;

If i give above like that, it should not work and please let me know how to resolve this issue?

Alex
Guest
Alex

Thank you very match!

nmn
Guest
nmn

you saved my day. thank you.

Mr Spoon
Guest
Mr Spoon

Thank you so much for this!
It works perfectly. Hibernate can be very unhelpful sometimes!

Rosdi
Guest
Rosdi

Isn’t that ridiculous? I think writing the actual sequence name in the generator field makes more sense.

trackback
Hibernate Tutorials | J2EE Web Development Tutorials

[…] org.hibernate.AnnotationException: Unknown Id.generator […]

software development company
Guest
software development company

Quite inspiring,

good tip:When we declared our annotation strategy to use “Sequences” as our id generator, we need to specify the @SequenceGenerator as well.
I try to remember it

Thanks for writing about it