JPA and Hibernate Basics
JPA and Hibernate Basics
JPA (Java Persistence API) is the standard specification for object-relational mapping in Java. Hibernate is the most popular implementation of that specification. Together they let you define entities as plain Java classes and let the framework handle all database interactions.
Defining an Entity
An entity is a Java class mapped to a database table. The @Entity annotation marks the class, @Table specifies the table name, and @Id marks the primary key:
import jakarta.persistence.*;
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 200)
private String title;
@Column(nullable = false)
private String author;
@Column(unique = true)
private String isbn;
private double price;
public Book() {}
public Book(String title, String author, String isbn, double price) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.price = price;
}
// getters and setters omitted for brevity
}
Key annotations: @Id marks the primary key. @GeneratedValue(strategy = GenerationType.IDENTITY) tells Hibernate the database uses auto-increment. @Column configures constraints and column names.
Entity Relationship Diagram
+-------------------------------------+
| books |
+-------------------------------------+
| * id BIGINT (PK, AUTO_INC) |
| title VARCHAR(200) NOT NULL |
| author VARCHAR(100) NOT NULL |
| isbn VARCHAR(20) UNIQUE |
| price DOUBLE |
+-------------------------------------+
Using EntityManager
EntityManager is the core JPA interface for database operations. You obtain it from an EntityManagerFactory (or injected via @PersistenceContext in a container):
@PersistenceContext
private EntityManager entityManager;
// Persist (insert) a new entity
Book book = new Book("Clean Code", "Robert Martin", "978-0132350884", 39.99);
entityManager.persist(book);
// Find an entity by primary key
Book found = entityManager.find(Book.class, book.getId());
System.out.println(found.getTitle()); // Clean Code
// Update an entity (just modify - flush handles the SQL)
found.setPrice(34.99);
entityManager.flush();
// Remove an entity
entityManager.remove(found);
Persistence Configuration
JPA requires a persistence.xml file (or Spring Boot auto-configuration) that defines the database connection and which entity classes to scan. In Spring Boot, a few properties in application.properties replace this entirely, but the underlying mechanism is the same.
When you call persist(), Hibernate generates an INSERT statement and sends it to the database. find() generates a SELECT. The mapping between your Java fields and database columns happens automatically based on your annotations.
Key Points
- JPA is the specification; Hibernate is the most popular implementation.
- Annotate a class with
@Entityand@Tableto map it to a database table. - Use
@Idand@GeneratedValueto define the primary key strategy. EntityManagerprovidespersist(),find(),merge(), andremove()for CRUD.@Columnlets you configure constraints likenullable,unique, andlength.