Hibernate is a practical Hibernate topic that becomes clear when you connect the definition to a small working example.
Use this page to understand what happens, why it happens, how to verify it, and what mistake usually breaks the concept.
After reading, practice Hibernate with a normal case, a boundary case, and a broken case so the idea becomes usable instead of memorized.
Hibernate Native SQL createNativeQuery should be studied as a practical Hibernate lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
In the hibernate > native-sql page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.
While HQL and Criteria API cover most use cases, native SQL is useful when:
import jakarta.persistence.*;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
@Repository
@Transactional
public class NativeSqlRepository {
@PersistenceContext
private EntityManager em;
// 1. Basic native query - returns Object[] rows
public List<Object[]> getRawUserData() {
return em.createNativeQuery(
"SELECT id, name, email FROM users WHERE active = 1")
.getResultList();
}
// 2. Map result directly to an entity class
@SuppressWarnings("unchecked")
public List<User> getActiveUsers() {
return em.createNativeQuery(
"SELECT * FROM users WHERE active = 1 ORDER BY name",
User.class) // Hibernate maps columns to entity fields
.getResultList();
}
// 3. Named parameters in native SQL
public List<User> getUsersByCity(String city) {
return em.createNativeQuery(
"SELECT * FROM users u " +
"JOIN addresses a ON u.id = a.user_id " +
"WHERE a.city = :city",
User.class)
.setParameter("city", city)
.getResultList();
}
// 4. Native query with pagination
public List<User> getUsersPaged(int page, int size) {
return em.createNativeQuery("SELECT * FROM users ORDER BY id", User.class)
.setFirstResult(page * size)
.setMaxResults(size)
.getResultList();
}
// 5. Native update/delete
public int deactivateOldUsers(int daysInactive) {
return em.createNativeQuery(
"UPDATE users SET active = 0 " +
"WHERE last_login < DATE_SUB(NOW(), INTERVAL :days DAY)")
.setParameter("days", daysInactive)
.executeUpdate();
}
// 6. Database-specific feature: MySQL window function
public List<Object[]> getUsersWithRank() {
return em.createNativeQuery(
"SELECT id, name, salary, " +
"RANK() OVER (ORDER BY salary DESC) AS salary_rank " +
"FROM users")
.getResultList();
}
}
import jakarta.persistence.*;
// Define named native queries on the entity
@Entity
@Table(name = "users")
@NamedNativeQuery(
name = "User.findActiveByRole",
query = "SELECT * FROM users WHERE active = 1 AND role = :role ORDER BY name",
resultClass = User.class
)
@NamedNativeQuery(
name = "User.getSummary",
query = "SELECT u.id, u.name, COUNT(o.id) AS order_count " +
"FROM users u LEFT JOIN orders o ON u.id = o.user_id " +
"GROUP BY u.id, u.name",
resultSetMapping = "UserSummaryMapping"
)
@SqlResultSetMapping(
name = "UserSummaryMapping",
classes = @ConstructorResult(
targetClass = UserSummaryDto.class,
columns = {
@ColumnResult(name = "id", type = Long.class),
@ColumnResult(name = "name", type = String.class),
@ColumnResult(name = "order_count", type = Long.class)
}
)
)
public class User {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String role;
private boolean active;
// getters/setters...
}
// Usage in repository:
// em.createNamedQuery("User.findActiveByRole", User.class)
// .setParameter("role", "ADMIN")
// .getResultList();
// DTO for custom result set mapping
public class UserSummaryDto {
private Long id;
private String name;
private Long orderCount;
// Constructor must match @ConstructorResult columns
public UserSummaryDto(Long id, String name, Long orderCount) {
this.id = id;
this.name = name;
this.orderCount = orderCount;
}
public Long getId() { return id; }
public String getName() { return name; }
public Long getOrderCount() { return orderCount; }
}
import jakarta.persistence.*;
@Repository
@Transactional
public class StoredProcedureRepository {
@PersistenceContext
private EntityManager em;
// Method 1: Using StoredProcedureQuery API
public List<User> callGetUsersByDept(String department) {
StoredProcedureQuery query = em
.createStoredProcedureQuery("get_users_by_dept", User.class);
// Register parameters
query.registerStoredProcedureParameter("dept_name", String.class,
ParameterMode.IN);
// Set parameter values
query.setParameter("dept_name", department);
// Execute and get results
query.execute();
return query.getResultList();
}
// Method 2: Procedure with OUT parameter
public int callGetUserCount(String role) {
StoredProcedureQuery query = em
.createStoredProcedureQuery("get_user_count");
query.registerStoredProcedureParameter("p_role", String.class, ParameterMode.IN);
query.registerStoredProcedureParameter("p_count", Integer.class, ParameterMode.OUT);
query.setParameter("p_role", role);
query.execute();
return (Integer) query.getOutputParameterValue("p_count");
}
// Method 3: Using native SQL CALL syntax
public void callArchiveOldOrders(int daysOld) {
em.createNativeQuery("CALL archive_old_orders(:days)")
.setParameter("days", daysOld)
.executeUpdate();
}
}
Hibernate should be learned as a practical Hibernate skill, not only as a definition. Start by asking what problem the topic solves, what input or state it receives, what rule it applies, and what visible result proves it worked.
A strong explanation of Hibernate includes the normal case, a boundary case, and a failure case. When you practice, write down the before-state, the operation, the after-state, and the reason the result changed.
This lesson was expanded because the audit reported: under 650 content words; limited checklist/practice/mistake/FAQ notes . The added notes below focus on clearer explanation, more examples, and concrete practice so the topic is easier to understand from the page itself.
Imagine you are adding Hibernate to a small learning project. The first step is to choose the smallest scenario that still shows the main idea. Avoid starting with a large production design; it hides the concept behind too many details.
Next, isolate the moving parts. Name the input, the rule, the output, and the possible error. This habit makes the topic easier to debug because you can see whether the problem is caused by bad data, wrong configuration, incorrect syntax, timing, permissions, or misunderstanding of the rule.
Finally, compare two versions: one correct version and one intentionally broken version. The broken version is valuable because it teaches you how the topic fails in real work, which is usually what interviews and debugging tasks test.
@Entity
@Table(name = "lesson_hibernate")
public class HibernateNote {
@Id
private Long id;
private String status;
public void markReviewed() {
this.status = "REVIEWED";
}
}
try (Session session = sessionFactory.openSession()) {
Transaction tx = session.beginTransaction();
HibernateNote note = session.find(HibernateNote.class, 1L);
note.markReviewed();
tx.commit();
}
// The important idea is to know when Hibernate tracks the object and when SQL is flushed.
Memorizing Hibernate as a definition only.
Pair the definition with a small working example and a failure example.
Copying syntax without checking the state before and after.
Write the input state, apply the rule, then inspect the output state.
Ignoring the error path for Hibernate.
Create one intentionally broken version and document the symptom and fix.
Memorizing Hibernate Native SQL createNativeQuery without the situation where it is useful.
Connect Hibernate Native SQL createNativeQuery to a concrete Hibernate task.
Understand the problem it solves, the input or state it works on, and the visible result that proves the concept is working.
Use one tiny correct example, one boundary example, and one broken example. Compare the output or state after each change.
They often memorize the term without tracing the behavior. Tracing makes the rule easier to remember and debug.
Remember the problem it solves in Hibernate, then attach the syntax or steps to that problem.
Explore 500+ free tutorials across 20+ languages and frameworks.