JPA Annotations — Quick overview

Andrei Baptista
2 min readMar 30, 2023

--

Java Persistence API (JPA) is a Java specification for Object-Relational Mapping (ORM) that allows Java developers to map Java classes to database tables. JPA provides a set of annotations that can be used to define the mapping between the Java objects and the database tables. Here are some of the most common JPA annotations:

  1. @Entity: This annotation is used to mark a Java class as an entity, which means that it is mapped to a database table. The class should have a no-argument constructor and private fields with public getters and setters.
  2. @Table: This annotation is used to specify the name of the database table that the entity is mapped to. By default, the table name is the same as the entity name.
  3. @Id: This annotation is used to mark a field as the primary key of the entity. The primary key can be generated by the database or set manually using other annotations.
  4. @GeneratedValue: This annotation is used to specify how the primary key is generated. There are several strategies available, including AUTO, IDENTITY, and SEQUENCE.
  5. @Column: This annotation is used to map a field to a database column. It can be used to specify the name of the column, its type, and other attributes such as length, nullable, and unique.
  6. @OneToMany: This annotation is used to specify a one-to-many relationship between two entities. It is typically used to map a collection of child entities to a parent entity.
  7. @ManyToOne: This annotation is used to specify a many-to-one relationship between two entities. It is typically used to map a single child entity to a parent entity.
  8. @ManyToMany: This annotation is used to specify a many-to-many relationship between two entities. It is typically used to map a collection of child entities to a collection of parent entities.

These are some of the most common JPA annotations that are used to map Java classes to database tables. There are many other annotations available in JPA that can be used to define more complex mappings and relationships between entities.

--

--

No responses yet