Skip to content

Module 1: Entities & JPA (The Warehouse Map)

📚 Module 1: Entities & JPA

Focus: Moving from “Java Objects” to “Database Rows.”

In Java, we use JPA (Java Persistence API) and Hibernate to talk to databases. Instead of writing SQL by hand, we create Entities.


🏗️ Step 1: The @Entity (The “Warehouse Map”)

An Entity is just a standard Java class that represents a table in your database.

🧩 The Analogy: The Warehouse Map

Imagine you have a warehouse.

  • The Database Table is a physical shelf.
  • The Entity Class is a drawing of that shelf in your notebook.
  • @Id: This is the unique barcode for every item on the shelf (Primary Key).

In Code:

@Entity
@Table(name = "products") // Name of the table in SQL
public class Product {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id; // Unique ID

    private String name;
    private Double price;
}

🏗️ Step 2: Hibernate (The “Invisible Translator”)

Hibernate is the tool that lives inside Spring Data JPA. It acts as a translator.

🧩 The Analogy: The Universal Translator

  • You speak Java (Objects).
  • The Database speaks SQL (Tables).
  • Hibernate listens to you and instantly translates: “Save this Product object” into “INSERT INTO products…”.

🏗️ Step 3: Why use JPA instead of raw SQL?

  1. Safety: If you misspell a column name, the Java compiler will tell you immediately.
  2. No SQL Injection: JPA handles security automatically.
  3. Portability: You can switch from MySQL to PostgreSQL without changing your Java code!

🥅 Module 1 Review

  1. JPA: The standard rulebook for saving data in Java.
  2. Hibernate: The actual engine that does the translation.
  3. @Entity: A class that maps to a table.
  4. @Id: The unique identifier for a row.