Page vs Slice
Spring Data provides two main interfaces for handling large data retrieval: Page<T> and Slice<T>.
Page
Provides pagination metadata. Getting total counts usually requires Hibernate to execute an additional, potentially heavy, COUNT query.
contenttotal elementstotal pagescurrent page
Slice
Focuses only on whether there is another slice/page available (ideal for "Infinite Scroll" UIs). It avoids the total-count requirement because it doesn't need to know the total pages or elements. For massive datasets, avoiding that extra count query makes a huge performance difference.
1. Complete Real-World Flow
Let's connect everything. Suppose we have a paginated read operation:
@Transactional
public Page<Employee> getEmployees(int page, int size) {
return employeeRepository.findAll(
PageRequest.of(page, size, Sort.by("salary").descending())
);
}
The complete architecture flow looks like this:
HTTP Request
↓
Controller
↓
Service
↓
@Transactional (Boundary opens)
↓
Repository
↓
Spring Data JPA
↓
EntityManager
↓
Hibernate (Generates JPQL/SQL)
↓
Database (Executes ORDER BY and LIMIT/OFFSET)
↓
Result Set
↓
Entity objects instantiated
↓
Persistence Context / First-Level Cache
↓
Service (Receives Page<Employee>)
↓
Controller
↓
HTTP Response
2. Complete Write Flow
For an update, the code is surprisingly simple:
@Transactional
public void updateSalary(Long id, double salary) {
Employee employee = entityManager.find(Employee.class, id);
employee.setSalary(salary);
}
The underlying flow is powerful:
@Transactional
↓
Begin transaction
↓
find()
↓
First-Level Cache / DB
↓
Managed Employee returned
↓
setSalary() (Modifies Java object)
↓
Dirty Checking (Hibernate detects the change)
↓
Flush (Hibernate generates UPDATE SQL)
↓
Commit (Database saves changes)
Notice you don't need to call entityManager.update(employee); because the entity is already managed.
3. Very Important Interview Flow
If an interviewer asks: "What happens when I change a field of an entity?"
Answer with this exact sequence:
- The entity is managed by the persistence context.
- The field is changed in Java memory.
- Hibernate's persistence context tracks the entity state.
- Dirty checking detects the change automatically.
- During flush, Hibernate generates the appropriate
UPDATESQL. - The SQL is executed within the active database transaction.
- The transaction commits, finalizing the save.
4. Most Important Interview Traps
| Trap | Reality |
|---|---|
| "Does save() always immediately execute INSERT?" | No. save() makes the entity managed. Timing of the actual INSERT depends on transaction boundaries, flush mode, and ID generation strategy. |
| "Is flush the same as commit?" | No. Flush = synchronize persistence context with DB. Commit = finalize transaction. |
| "Does flush permanently save data?" | Not by itself. A flush sends SQL, but a later rollback can still revert those changes if the transaction fails. |
| "JPQL uses table names." | No. JPQL uses Entity class names and Entity attributes (fields). |
| "HQL and JPQL are exactly the same." | No. JPQL is the JPA standard. HQL is Hibernate's specific query language (which includes JPQL but adds extra vendor features). |
| "Updating a managed entity requires save()." | Not necessarily. Dirty checking detects the change and flushes it automatically at the end of the transaction. |
| "@Transactional means SQL is executed immediately." | No. It establishes transaction boundaries. Hibernate still controls exactly when the flush happens. |
5. Your Hibernate CRUD + Query Mental Map
You should now be able to visualize the whole Spring Boot + Hibernate ecosystem like this:
Spring Boot
|
@Transactional
|
EntityManager
|
┌──────────┼──────────┐
↓ ↓ ↓
persist() find() remove()
| | |
└──────────┼──────────┘
↓
Persistence Context
|
First-Level Cache
|
Dirty Checking
|
flush()
|
SQL
┌──────────┼──────────┐
↓ ↓ ↓
INSERT UPDATE DELETE
|
Transaction
|
commit()
|
Database
And for querying: Understand how JPQL/HQL operates on entities, and how sorting and pagination push the heavy lifting down to the database to preserve Java memory.
Interactive Look: The Complete Lifecycle
Step through this interactive flowchart to visually reinforce the mental map you just read. Click through the layers to see how a request flows from the Controller, through the Persistence Context, and down to the Database.
⭐ What you should master for T3
For this Hibernate section, don't just memorize annotations. Be able to confidently explain this exact sequence:
- Entity state
- Persistence Context
- First-Level Cache
- Dirty Checking
- Flush
- SQL
- Transaction
- Commit / Rollback
Comments (0)