org.hibernate.AnnotationException: No identifier specified for entity

When you are writing a piece of code from scratch a lot of time you do silly mistakes and still wonder why its not working. Well same thing happened the other day when I added a Hibernate Entity class in one project and was struggling to make it work.

The exception was:


org.hibernate.AnnotationException: No identifier specified for entity: net.viralpatel.hibernate.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 error here is that in your Entity class, you have not defined a primary key. Thus specify either @Id annotation or an @EmbeddedId annotation.

So if you have an entity class Employee like below:

package net.viralpatel.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
 
@Entity
@Table(name="EMPLOYEE")
public class Employee {
 
    @Column(name="employee_id")
    private Long employeeId;
 
    @Column(name="firstname")
    private String firstname;
 
    @Column(name="lastname")
    private String lastname;
    

    //Getter and Setter methods	

}

And if you try to execute this, it will generate exception org.hibernate.AnnotationException: No identifier specified for entity: net.viralpatel.hibernate.Employee

So the solution is just add @Id to appropriate primary key column.

package net.viralpatel.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;

 
@Entity
@Table(name="EMPLOYEE")
public class Employee {
 
    @Id
    @Column(name="employee_id")
    private Long employeeId;
 
    @Column(name="firstname")
    private String firstname;
 
    @Column(name="lastname")
    private String lastname;
    

    //Getter and Setter methods	

}

Thus every class defined as Entity with @Entity annotation, needs an @Id or @EmbeddedId property.

Hope that helps and reduce your debugging effort.



21 Comments

  • Pramod 26 October, 2012, 12:58

    Thanks Viral.
    Your tutorial really helped me save another nightout.
    cheers to you!

  • Hernan 2 November, 2012, 4:51

    Thanks a lot!!!

  • prateek 22 January, 2013, 20:11

    Thanks viral,

    your suggestion has worked for me.

  • Hardik 14 February, 2013, 11:33

    Thank you

  • Daniel Winterstein 19 February, 2013, 20:11

    And if you are sub-classing, there is a another silly mistake you (or at least I) can make which could produce the same exception.

    Which is, the super-class needs to have the @MappedSuperclass annotation, otherwise all it’s fields/columns are ignored.

  • Mridula 16 April, 2013, 14:04

    Thanks for this.
    If my entity class does not have any primary key, then what can be done to resolve this issue?

    • Alen Jain 21 December, 2013, 22:32

      in hibernate it is mandatory to have at least one candidate key per table.

  • apoorv 17 June, 2013, 14:40

    but my table doesnt have a primry key but has a foreign key how to do the same with annotations

  • Cristian Daniel Ortiz Cuellar 25 September, 2013, 2:13

    Thanks to Daniel Winterstein i had a series of abstract class and one @MappedSuperClass was missing thanks al ot.

  • mahender 1 October, 2013, 16:07

    thank u

  • Amar 9 October, 2013, 9:09

    Thank you…This is what exactly I was looking for..you made my day..Thanks again :)

  • sonam 17 October, 2013, 16:41

    thank u

  • Ahmad 11 December, 2013, 15:26

    Nice, Saved my day!

  • Resmi 13 December, 2013, 13:35

    Thanks A lot!!!

  • venky 27 February, 2014, 18:27

    Thanks Viral

  • hanita 20 April, 2014, 15:23

    think u very much u saved my day :)

  • ricardo 15 May, 2014, 14:07

    yup, this save my day too. a big thank you.

  • Chintan 15 May, 2014, 15:30

    thanks .
    It worked

  • suresh 27 May, 2014, 19:59

    Sir,
    I have been searching for the solution for the long time.
    LOve you…..

  • benoyprakash 26 June, 2014, 23:54

    Thank you Boss.

    I was having the errors:
    javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    and
    org.hibernate.AnnotationException: No identifier specified for entity: com.classifieds.entity.Test

    But i didnt notice the second one. Actually the @Id missing was the issue.

  • Jonathan 2 July, 2014, 16:33

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current ye@r *