In this tutorial, I will cover one of the basic annotations to work with mapping the columns of tables in the database, that is the @Id annotation in JPA.
We will go straight into the problem.
This annotation is declared to determine where the primary key attribute of the entity corresponds to the primary key column in the table. For example, we usually take the column id as the primary key in the table, when mapping with the Java entity object, we will add the @Id annotation associated with the corresponding column id attribute.
For example, we have the following entity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.huongdanjava.jpa; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import lombok.Getter; import lombok.Setter; @Entity @Table @Getter @Setter public class Clazz { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String name; } |
An entity must have at least one primary key attribute associated with @Id annotation. If you do not declare any @Id annotation, when running you will encounter errors immediately.
For example, if I now comment on the @Id annotation of the id attribute in Clazz class, following error will happen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Exception in thread "main" javax.persistence.PersistenceException: Unable to build entity manager factory at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:66) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) at com.huongdanjava.jpa.Application.main(Application.java:14) Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.huongdanjava.jpa.Clazz at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:265) at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:712) at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:876) at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58) ... 3 more |
We can use multiple Java data types for the primary key attribute of the entity. As follows:
- Primitive: byte, int, short, long, char.
- Wrapper of primitive: Byte, Integer, Short, Long, Character.
- String, BigInteger.
- java.util.Date, java.sql.Date
In addition, we can also use floats and doubles as well as wrappers for these primitives, such as Float, Double, BigDecimal, but these data types should not be used because these are decimal values, easy to have error when rounding.