Skip to content
Home » object creation in hibernate

object creation in hibernate

Below is a clear, structured, and detailed explanation of Object Creation in Hibernate, presented in a technical and concept-oriented manner for strong understanding.


Object Creation in Hibernate

Introduction

In Hibernate, object creation refers to the process of creating a Java object (POJO) and persisting it into the database. Unlike JDBC, where developers write SQL queries manually, Hibernate allows developers to work with objects, and it automatically converts them into database records.

This process is part of Hibernate’s Object-Relational Mapping (ORM) mechanism.


Concept of Object Creation

In Hibernate:

  • A Java object represents a row in a database table
  • Creating an object in Java → Inserting a record in database
  • Object state is managed by Hibernate

Steps for Object Creation in Hibernate

Step 1: Create POJO Class (Entity Class)

@Entity
@Table(name="student")
public class Student {

    @Id
    private int id;
    private String name;

    // getters and setters
}

Step 2: Configure Hibernate

Configuration is done using hibernate.cfg.xml.

<hibernate-configuration>
    <session-factory>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/testdb
        </property>
        <mapping class="Student"/>
    </session-factory>
</hibernate-configuration>

Step 3: Create SessionFactory

Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();

Step 4: Open Session

Session session = factory.openSession();

Step 5: Begin Transaction

Transaction tx = session.beginTransaction();

Step 6: Create Object

Student s = new Student();
s.setId(1);
s.setName("John");

Step 7: Save Object

session.save(s);

This step:

  • Converts object → SQL INSERT statement
  • Stores data in database

Step 8: Commit Transaction

tx.commit();

Step 9: Close Session

session.close();

Complete Example

Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();

Session session = factory.openSession();
Transaction tx = session.beginTransaction();

Student s = new Student();
s.setId(1);
s.setName("John");

session.save(s);

tx.commit();
session.close();

Object States in Hibernate

During object creation, Hibernate manages objects in different states:

1. Transient State

  • Object is created using new
  • Not associated with Hibernate session
  • Not stored in database
Student s = new Student();

2. Persistent State

  • Object is associated with session
  • Stored in database
session.save(s);

3. Detached State

  • Object was persistent but session is closed
  • Not currently managed by Hibernate
session.close();

Methods Used for Object Creation

MethodDescription
save()Saves object and returns ID
persist()Saves object (JPA standard)
saveOrUpdate()Saves or updates based on state

Key Features of Hibernate Object Creation

  • No need to write SQL
  • Automatic table mapping
  • Transaction management
  • Supports cascading operations
  • Works with POJO classes

Advantages

  • Simplifies database operations
  • Reduces coding effort
  • Improves productivity
  • Maintains object-oriented approach
  • Handles SQL internally

Common Errors

  • Missing configuration file
  • Incorrect mapping
  • No primary key defined
  • Not committing transaction
  • Session not closed

Best Practices

  • Always use transactions
  • Close session properly
  • Use persist() in modern applications
  • Validate object before saving
  • Use DAO pattern

Conclusion

Object creation in Hibernate is a fundamental operation where Java objects are mapped and persisted into database tables. By using Hibernate’s session and transaction management, developers can efficiently create and store objects without writing SQL queries. Understanding object states and lifecycle is crucial for building robust and scalable Hibernate-based applications.