List of Challenges in Hibernate Solves in ORM

  1. Problem of Identity

  2. Problem of Granularity

  3. Problem of Inheritance

  4. Problem of Associations

  5. Problem of Navigation

  6. Problem of Object Lifecycle

  7. Problem of Data Duplication

  8. Problem of Lazy Loading / Performance

  9. Problem of Object Graphs and Cycles

  10. Problem of Data Type Mismatch

  11. Problem of Transactions

  12. Problem of Concurrency

  13. Problem of Querying

  14. Problem of Cascading Operations

  15. Problem of Batching & N+1 Queries

  16. Problem of Schema Evolution

  17. Problem of Detached Entities


1. The Problem of Identity

This problem arises when the same database row is read multiple times, creating different object instances in Java. In such cases, Java treats them as different because they are separate objects in memory.

User u1 = new User();
u1.setId(1L);

User u2 = new User();
u2.setId(1L);

System.out.println(u1 == u2); // ❌ false

How Hibernate Solved this Problem

User u1 = entityManager.find(User.class, 1L);
User u2 = entityManager.find(User.class, 1L);

System.out.println(u1 == u2); // ✅ true in Hibernate (if same session)

2. Problem of Granularity

Java supports rich, fine-grained objects (like nested objects, classes within classes),
but relational databases store data in flat tables with primitive fields only.

class Address {
    private String street;
    private String city;
    private String zipCode;
}

@Entity
class User {
    @Id
    private Long id;
    private String name;

    private Address address; // ❗ Complex object, not a primitive
}

#Solution Hibernate introduces:

🔹 @Embeddable and @Embedded

  • You mark the nested class (Address) as @Embeddable

  • And use @Embedded in the parent entity

@Embeddable
public class Address {
    private String street;
    private String city;
    private String zipCode;
}


@Entity
public class User {
    @Id
    private Long id;
    private String name;

    @Embedded
    private Address address;
}