org.hibernate.AnnotationException: No identifier specified for entity

org.hibernate.AnnotationException: No identifier specified for entity

Posted: 10 May 2012 04:53 AM PDT

Source site: Click here

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值