1.
Association Mapping in ORM
What is Association Mapping?
In real-world applications, entities are not isolated. They are related to each other. For example:
• A Student belongs to a Department
• An Order has multiple Products
• A User has multiple Addresses
In relational databases, these relationships are represented using foreign keys.
In object-oriented programming, relationships are represented using object references.
Association mapping in Hibernate/JPA defines how these relationships between entities are mapped to
database tables.
Types of Association Mapping
One-to-One
One entity is associated with exactly one other entity.
Example:
• One User → One Profile
@OneToOne
@JoinColumn(name="profile_id")
private Profile profile;
In the database:
• Foreign key column connects both tables.
One-to-Many
One entity is associated with multiple entities.
Example:
• One Department → Many Students
@OneToMany(mappedBy="department")
private List<Student> students;
Database:
• Foreign key stored in Student table.
Many-to-One
Many entities relate to one entity.
Example:
• Many Orders → One Customer
@ManyToOne
@JoinColumn(name="customer_id")
private Customer customer;
Many-to-Many
Multiple entities relate to multiple entities.
Example:
• Students ↔ Courses
@ManyToMany
@JoinTable(
name="student_course",
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="course_id")
)
private List<Course> courses;
Database:
• A join table is created.
Why Association Mapping is Important
• Represents real-world relationships
• Maintains referential integrity
• Simplifies data navigation
• Supports lazy and eager loading
2. Inheritance Mapping
What is Inheritance Mapping?
In object-oriented programming, classes can inherit from other classes. But relational databases do not
support inheritance directly.
Hibernate provides strategies to map inheritance hierarchies to relational tables.
Inheritance Strategies
Single Table Strategy
All classes stored in one table.
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
Advantages:
• Simple
• Fast queries
Disadvantages:
• Many null columns
Joined Table Strategy
Each class has its own table.
@Inheritance(strategy=[Link])
Advantages:
• Normalized database
• No null columns
Disadvantages:
• More joins
Table Per Class Strategy
Each class has separate table with full fields.
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
Advantages:
• No joins
Disadvantages:
• Data duplication
Why Inheritance Mapping Matters
• Preserves OOP design
• Maintains database structure
• Supports polymorphism
3. Hibernate Session and Transaction
What is a Session?
A Session represents a connection between application and database.
It is used to:
• Save objects
• Update objects
• Delete objects
• Retrieve objects
Example:
Session session = [Link]();
What is a Transaction?
A Transaction ensures data consistency.
Properties:
• Atomicity
• Consistency
• Isolation
• Durability (ACID)
Example:
Transaction tx = [Link]();
[Link](student);
[Link]();
Without transaction:
• Data may become inconsistent
• Partial updates may occur
Session Lifecycle
1. Open Session
2. Begin Transaction
3. Perform operations
4. Commit/Rollback
5. Close Session
4. Caching in Hibernate
Why Caching?
Frequent database access reduces performance.
Hibernate provides caching to:
• Reduce database hits
• Improve performance
• Increase scalability
Types of Cache
First-Level Cache (Session Cache)
• Default
• Stored in Session
• Automatically enabled
Second-Level Cache
• Shared across sessions
• Optional
• Configurable
Query Cache
• Stores query results
Benefits of Caching
• Faster performance
• Reduced database load
• Improved scalability
5. HQL (Hibernate Query Language)
What is HQL?
HQL is an object-oriented query language used to query Hibernate entities.
Unlike SQL:
• HQL works on objects
• SQL works on tables
HQL Example
Query query = [Link]("from Student where name=:name");
[Link]("name","Ravi");
List<Student> list = [Link]();
HQL Advantages:
• Database independent
• Cleaner syntax
• Works with entity classes
6. Native Query
What is Native Query?
Native query allows writing pure SQL queries inside Hibernate.
Used when:
• Complex SQL needed
• Database-specific features required
Example:
Query query = [Link]("SELECT * FROM student");
Difference Between HQL and Native Query
Feature HQL Native Query
Works On Objects Tables
Database Independent Yes No
Syntax Java-based SQL-based