Even sometimes, I do this type of silly mistakes and wonder why its not working. Generally, these type of problems occur when we write code from scratch and same thing happened with my colleague.
org.hibernate.AnnotationException: No identifier specified for entity: com.gsms.core.dao.entity.Employee at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:650) at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:498) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:277) at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1112) at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1269) at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:150) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:888) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:416) at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:126) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:227) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:273) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1333) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) at java.security.AccessController.doPrivileged(Native Method)
The root cause of this error is that you forgot to define primary key (or identity field) in your entity class. It is mandatory to specify the identity field in hibernate. You have to specify either @Id annotation (if single primary key) or @EmbeddedId annotation (if composite primary key).
Below is a sample entity class by using which I'll explain the problem. I created a new project for giving example as I can't share my project code.
package com.gsms.core.dao.entity.Employee; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import lombok.Getter; import lombok.Setter; import javax.persistence.Id; @Entity @Table(name="EMPLOYEE") public class Employee { @Getter @Setter @Column(name="employee_id") private Long employeeId; @Getter @Setter @Column(name="first_name") private String firstName; @Getter //It generates getter method automatically. @Setter //It generates setter method automatically. @Column(name="last_name") private String lastName; }
If you try to run this code then it will generate exception org.hibernate.AnnotationException: No identifier specified for entity: com.gsms.core.dao.entity.Employee
So the solution is add either @Id annotation (if single primary key) or @EmbeddedId annotation (if composite primary key).
package com.gsms.core.dao.entity.Employee; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import lombok.Getter; import lombok.Setter; import javax.persistence.Id; @Entity @Table(name="EMPLOYEE") public class Employee { @Id @Getter @Setter @Column(name="employee_id") private Long employeeId; @Getter @Setter @Column(name="first_name") private String firstName; @Getter //It generates getter method automatically. @Setter //It generates setter method automatically. @Column(name="last_name") private String lastName; }
Thus, every class defined as Entity with @Entity annotation, needs either @Id annotation (if single primary key) or @EmbeddedId annotation (if composite primary key).
So, I would suggest that when you write a new entity class then check that you have defined primary key (@Id or @EmbeddedId annotation) in entity class.
Hope, this information would help to understand the problem and its consequences and reduce your debugging efforts. Please feel free to post any comment or question.
0 comments