Hibernate & JPA Basics

    Atul Kabra4 min readUpdated

    Hibernate is an Object-Relational Mapping (ORM) framework that lets you work with database data as ordinary Java objects, while JPA (Jakarta Persistence API) is the standard specification Hibernate implements. Together they let you save, load, and query database rows by manipulating Java objects — no hand-written SQL for routine operations. Instead of mapping result sets column by column (as you do in raw JDBC), you annotate a class as an entity and Hibernate handles the translation between objects and tables both ways.

    This saves enormous amounts of repetitive code and is the standard data layer for Spring Boot apps.

    What an ORM does

    An ORM bridges two worlds: object-oriented Java and relational tables. A Student object should map to a row in a students table; its fields map to columns. The ORM:

    • Turns a saved object into an INSERT/UPDATE.
    • Turns a loaded row back into an object.
    • Manages relationships (a student has many enrollments) as object references.
    • Tracks changes and generates the SQL automatically.

    JPA defines the standard annotations and behaviour; Hibernate is the engine that actually does the work.

    Defining an entity

    You mark a class as a database entity with Jakarta Persistence annotations (note the jakarta.persistence package — the old javax.persistence was renamed under Jakarta EE):

    import jakarta.persistence.Entity;
    import jakarta.persistence.GeneratedValue;
    import jakarta.persistence.GenerationType;
    import jakarta.persistence.Id;
    import jakarta.persistence.Table;
    import jakarta.persistence.Column;
    
    @Entity                       // marks this class as a JPA entity
    @Table(name = "students")     // maps it to the "students" table
    public class Student {
    
        @Id                                              // the primary key
        @GeneratedValue(strategy = GenerationType.IDENTITY) // DB auto-generates the id
        private Long id;
    
        @Column(nullable = false) // maps to a NOT NULL column named "name"
        private String name;
    
        @Column(unique = true)    // enforces a unique email column
        private String email;
    
        // JPA requires a no-argument constructor
        protected Student() { }
    
        public Student(String name, String email) {
            this.name = name;
            this.email = email;
        }
    
        // getters and setters omitted for brevity
    }
    

    That is all Hibernate needs to read and write Student rows. No SQL, no manual column mapping.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Using it with Spring Data JPA

    In Spring Boot you rarely call Hibernate directly. Instead you declare a repository interface and Spring generates the implementation:

    import org.springframework.data.jpa.repository.JpaRepository;
    import java.util.Optional;
    
    // Extending JpaRepository gives save, findById, findAll, delete, and more — for free
    public interface StudentRepository extends JpaRepository<Student, Long> {
    
        // Spring derives the query from the method name automatically
        Optional<Student> findByEmail(String email);
    }
    

    You never write the SQL for findByEmail — Spring Data parses the method name and Hibernate generates the query. This is the everyday way most Java apps access data today.

    Managing relationships

    Real models have relationships. JPA expresses them with annotations like @OneToMany, @ManyToOne, and @ManyToMany. For example, a Student could have a @OneToMany list of Enrollment objects, and Hibernate maps that to the foreign-key relationship in the database. Start simple with single entities before adding relationships.

    Common mistakes

    • Using javax.persistence annotations. Modern Hibernate and Spring Boot use jakarta.persistence. Mixing them causes mapping failures.
    • Forgetting the no-arg constructor. JPA needs one to instantiate entities; without it Hibernate cannot create objects from rows.
    • The N+1 query problem. Lazily loading a collection inside a loop fires one query per item. Learn to use fetch joins or @EntityGraph to load related data efficiently.
    • Putting business logic in entities. Entities should model data; keep logic in services so persistence concerns stay isolated.
    • Treating Hibernate as a black box. Enable SQL logging while learning so you can see the queries Hibernate generates and catch inefficiencies early.

    FAQ

    Hibernate vs JPA — what is the difference? JPA is the specification (the standard interfaces and annotations). Hibernate is the most popular implementation of that specification. You code against JPA; Hibernate runs it.

    Do I still need to know SQL? Yes. ORMs handle routine cases, but understanding SQL and JDBC helps you debug performance and write efficient custom queries.

    Keep learning

    Hibernate sits above JDBC and powers the data layer of a REST API built on Spring Boot. Explore more in the Advanced Java hub.


    Want to master Java persistence the modern way? Join the waitlist for the Advanced Java course at Infoplanet, Jalgaon.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides