MODULE 4 – ADVANCED ORM FEATURES
1. Batch Processing in Hibernate
What is Batch Processing?
Batch processing refers to executing multiple database operations together in a single batch instead
of executing them one by one.
Normally, if we insert 1000 records:
• Hibernate sends 1000 individual SQL statements.
• This causes heavy database overhead.
With batch processing:
• Hibernate groups multiple statements.
• Sends them in batches.
• Improves performance significantly.
Why Batch Processing is Needed
• Improves performance for bulk operations
• Reduces database round trips
• Suitable for large data import/export
• Used in enterprise data migration
How to Enable Batch Processing
In [Link]:
<property name="[Link].batch_size">20</property>
This means:
• Hibernate sends 20 insert statements per batch.
Example Code
for(int i=1; i<=1000; i++){
Student s = new Student();
[Link]("Student " + i);
[Link](s);
if(i % 20 == 0){
[Link]();
[Link]();
}
}
flush() → sends batch to database
clear() → clears session cache
Benefits of Batch Processing
• Faster execution
• Reduced memory usage
• Suitable for large-scale systems
2. Intercepting Filter (Conceptual Layered Design)
What is Intercepting Filter?
Intercepting Filter is a design pattern used to preprocess or postprocess requests before reaching the
main business logic.
In Hibernate/Spring context:
• Used for logging
• Security checks
• Validation
• Performance monitoring
Where It Is Used
• Web layer (Servlet Filters)
• Hibernate interceptors
• Spring AOP
Hibernate Interceptor Example
public class MyInterceptor extends EmptyInterceptor {
public boolean onSave(Object entity, Serializable id,
Object[] state, String[] propertyNames,
Type[] types) {
[Link]("Entity Saved: " + entity);
return false;
}
}
This allows:
• Monitoring entity lifecycle
• Auditing
• Custom validation
Why Intercepting Filter Is Important
• Centralized request handling
• Cleaner architecture
• Improves modularity
• Enhances security
3. Criteria Builder (JPA Criteria API)
What is Criteria Builder?
Criteria Builder is part of JPA used to build type-safe, dynamic queries programmatically.
Unlike HQL:
• Criteria API is Java-based
• Queries are built dynamically
• Safer and less error-prone
Why Use Criteria Builder?
• Dynamic query construction
• Compile-time checking
• Avoids string-based query errors
• Useful for complex filtering
Example
CriteriaBuilder cb = [Link]();
CriteriaQuery<Student> cq = [Link]([Link]);
Root<Student> root = [Link]([Link]);
[Link](root)
.where([Link]([Link]("name"), "Ravi"));
List<Student> results = [Link](cq).getResultList();
This query fetches students where name = Ravi.
Advantages
• Type safety
• Dynamic filtering
• Secure
• Avoids SQL injection
4. Projections API
What is Projection?
Projection means selecting specific columns instead of entire entity objects.
Instead of:
from Student
We can select:
select name from Student
Why Projection is Important
• Reduces data transfer
• Improves performance
• Fetch only required fields
Example (HQL Projection)
Query query = [Link]("select [Link] from Student s");
List<String> names = [Link]();
Types of Projection
1. Single column projection
2. Multiple column projection
3. Aggregate projection (count, sum, avg)
Example:
select count([Link]) from Student s
5. Named Query
What is Named Query?
Named Query is a predefined query defined at entity level.
It improves:
• Reusability
• Performance
• Readability
Example
@NamedQuery(
name="[Link]",
query="from Student where name=:name"
)
Using it:
Query query = [Link]("[Link]");
[Link]("name", "Ravi");
Advantages
• Defined once, reused many times
• Easy maintenance
• Precompiled
6. Native Query
What is Native Query?
Native Query allows executing raw SQL inside Hibernate.
Used when:
• Complex joins needed
• Database-specific features required
Example:
Query query = [Link](
"SELECT * FROM student"
);
Named Native Query
Defined using annotation:
@NamedNativeQuery(
name="[Link]",
query="SELECT * FROM student"
)
7. Framework Integration (Spring Data JPA)
Role of Framework
Spring Data JPA simplifies ORM by:
• Removing DAO boilerplate code
• Auto-generating queries
• Integrating with Spring Boot
Example Repository:
public interface StudentRepository
extends JpaRepository<Student, Long> {
}
No need to write:
• Session code
• Transaction code
• CRUD methods
Why Framework Integration Matters
• Cleaner architecture
• Faster development
• Production-ready applications
• Microservices friendly