VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/entities-and-annotations-entity-id-table-in-jpa/

⇱ Entities and Annotations (@Entity, @Id, @Table) in JPA - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Entities and Annotations (@Entity, @Id, @Table) in JPA

Last Updated : 23 Aug, 2025

JPA (Java Persistence API) is a specification in Java that simplifies database interactions by mapping Java classes to relational database tables. An @Entity annotation marks a class as an entity, where each instance of the class represents a row in the table. JPA uses annotations such as @Entity, @Id and @Table to define this mapping.

We will cover three main annotations used in JPA:

  • @Table
  • @Entity
  • @Id

@Table Annotation

The @Table annotation in JPA is used to define the database table mapping for an entity. It allows customization of:

  • Table name (default is the entity class name)
  • Catalog and schema (useful for multi-database environments)
  • Unique constraints on specific columns

Syntax

Attributes of @Table Annotation

The @Table annotation provides the following attributes:

  • name: Defines the table name (default: entity class name).
  • catalog: Specifies the database catalog.
  • schema: Defines the database schema.
  • uniqueConstraints: Enforces unique constraints on specific columns

@Entity Annotation

The @Entity annotation of the Java class can indicate that it is the entity and it can represent the tables in the relational database. When a class is annotated with @Entity, JPA treats it as a persistent Java object whose state can be stored in and retrieved from a database.

Key points:

  • Must be placed at the class level.
  • The class should have a no-argument constructor.
  • Must have a primary key field marked with @Id.

Syntax

@Id Annotation

The @Id annotation in JPA marks a field as the primary key of an entity. It uniquely identifies each row in the corresponding database table and is mandatory for every entity

Syntax


The @GeneratedValue annotation defines how primary key values are automatically generated.

Example: GenerationType.IDENTITY (auto-increment), GenerationType.SEQUENCE or GenerationType.AUTO

Step-by-Step Implementation Example

Step 1: Create a Maven project in IntelliJ IDEA

  • File ->New-> Project ->Maven
  • Check “Create from archetype” off (simple project)
  • GroupId: org.example
    ArtifactId: create-entity-demo
  • Language level: Java 11 (or 17)

Step 2: Verify the project structure

Once the project then the file structure looks like the image below.

👁 output
output

Step 3: Add dependencies (pom.xml)

Use Hibernate 6, MySQL 8 and JAXB (for XML handling). Also set the compiler to Java 11

pom.xml:

Step 4: Create persistence.xml

Open the persistence.xml and put the below code into the project and it can configure the database of the project.

Step 5: Create the Entity (@Entity, @Id, @Table)

  • The Product class is a JPA entity that maps to the products table in the database.
  • It represents a product with fields for id, name and price, where id is the primary key and is auto-generated.

Product.java

Step 6: Create MainApp class

  • The MainApp class demonstrates basic JPA operations like persisting and retrieving Product entities using EntityManager.
  • It inserts sample data into the database and then queries and displays all stored products.

MainApp.java:

Step 7: Run the application

In IntelliJ: Right-click MainApp-> Run.

👁 output
output

The application starts by creating an EntityManager, persists two Product entries into the database and then retrieves and displays them. It uses JPA's transaction management and JPQL to handle database operations.

Comparison table of @Entity,@Id and @Table

Annotation

Purpose

mandatory

Common attribute

@Entity

 it can represent the tables in the relational database

yes

name (optional for JPQL alias)

@Table

Specifies the table name and other table-level properties

No

name, schema, catalog, uniqueConstraints

@Id

Marks a field as the primary key of the entity

yes

(No attributes directly; usually combined with @GeneratedValue)

Comment
Article Tags:

Explore