Skip to content
QuizMaker logoQuizMaker
Activity
Java Backend Interview Prep

No lessons available

CONTENTS

4. Hibernate ORM Interview Essentials

Review ORM, SessionFactory, Session, transactions, HQL, and Criteria API.

Java Backend Interview Prep
1. Java Frameworks & Testing
May 29, 2026
21
A

What Hibernate Solves

Hibernate is an ORM framework. ORM maps relational database tables to Java objects so developers can work with entities instead of manually writing every JDBC mapping step.

Core Interfaces

InterfaceRole
ConfigurationReads Hibernate configuration
SessionFactoryThread-safe factory for Session objects
SessionUnit of work for interacting with the database
TransactionGroups database operations atomically
QueryExecutes HQL/JPQL-style queries
CriteriaBuilds dynamic queries through an API

SessionFactory vs Session

SessionFactory is heavyweight and thread-safe. It is usually created once per application. Session is lightweight but not thread-safe and should be short-lived.

ORM vs JDBC

Hibernate can reduce boilerplate, manage entity state, and hide repetitive SQL mapping. JDBC gives more direct control and may be simpler for small, explicit queries.

Hibernate Code Examples

Entity Mapping

@Entity
@Table(name = "orders")
public class OrderEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String customerEmail;

    @Column(nullable = false)
    private BigDecimal totalAmount;
}

Repository Style

@Repository
public class OrderRepository {
    @PersistenceContext
    private EntityManager entityManager;

    public OrderEntity findById(Long id) {
        return entityManager.find(OrderEntity.class, id);
    }

    public void save(OrderEntity order) {
        entityManager.persist(order);
    }
}

Session and Transaction Style

try (Session session = sessionFactory.openSession()) {
    Transaction tx = session.beginTransaction();
    OrderEntity order = session.get(OrderEntity.class, orderId);
    order.setTotalAmount(newTotal);
    tx.commit();
}

Interview Scenario Practice

Scenario 1: Shared Static Session

Scenario: A team stores a Hibernate Session in a static field and shares it across web requests.

Strong answer: Share SessionFactory, not Session. A SessionFactory is thread-safe and expensive to create; a Session is short-lived and not thread-safe.

Why it works: Each request or transaction gets its own unit of work while the factory remains reusable.

Common mistake: Treating Session like a global database connection.

Scenario 2: Optional Search Filters

Scenario: A search API has optional filters such as status, date range, customer email, and minimum amount.

Strong answer: Use Criteria-style query building or a repository abstraction that composes predicates safely.

Why it works: Dynamic filters can be added conditionally without fragile string concatenation.

Common mistake: Building query strings by manually concatenating user input, which risks bugs and injection issues.

Scenario 3: ORM Performance Issue

Scenario: A page becomes slow after adding Hibernate relationships.

Strong answer: Check generated SQL, lazy/eager loading, N+1 queries, indexes, and transaction boundaries. Hibernate reduces boilerplate but does not remove database performance work.

Why it works: ORM problems usually appear in the SQL and data access pattern, not only in Java code.

Common mistake: Saying "Hibernate is slow" without inspecting the actual queries.

Share this article

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava Backend Interview Prep
Quiz: Hibernate ORM
11 questions8 min

0 comments

Please login to comment.
No comments yet.
Lesson 4 of 5 in 1. Java Frameworks & Testing
Previous in 1. Java Frameworks & Testing
3. Spring Boot REST APIs and HTTP Codes
Next in 1. Java Frameworks & Testing
5. Testing: JUnit, Assertions, Mock, and Spy
Back to Java Backend Interview Prep
Back to moduleCategories