Hibernate : How to use @Id annotation

Points to remember

  • There should be only one @Id annotation in a hibernate entity class.
  • There should be at least one field specified as primary id.
  • You can use different approaches for creating primary id using these Primary Key Generation Strategies.

How to use @Id annotation in Hibernate

Taking the example in the previous example@Id annotation tells the hibernate to

@Id annotation tells the hibernate that this field will be the primary key of the table.

@Id annotation class looks like the following
@Target({METHOD, FIELD})
@Retention(RUNTIME)

public @interface Id {
}

If we do not add @Id annotation on any of the fields in Hibernate entity then you will get the following error.

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249)
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:860)
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
 at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
 at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
 at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
 ... 15 more
 Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.ekiras.domian.User
 at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
 at org.hibernate.cfg.InheritanceState.getElementess(InheritanceState.java:224)
 at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)
 at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
 at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)
 at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
 ... 23 common frames omitted

No comments :

Post a Comment