1. Import Hibernate Template Project
- Downloads Hibernate JARs in Maven local repo
2. Configure Hibernate
- Copy (or create)
hibernate.cfg.xml
in <resources>
directory
- Configure database settings
Important Hibernate Property:
<property name="hibernate.hbm2ddl.auto">update</property>
This property enables Hibernate to:
- Create a NEW table if none exists for the POJO (Entity class)
- Otherwise continue with the existing table
3. Create HibernateUtils Class
- Create singleton instance of the SessionFactory (SF) using a static init block
Key APIs:
-
Create Configuration instance:
Configuration config = new Configuration();
-
Configure it:
public Configuration configure() throws HibernateException
-
Build SessionFactory:
public SessionFactory buildSessionFactory() throws HibernateException
- Note:
SessionFactory
extends AutoCloseable
4. Write a Tester
- Create main class to test the initialization of the Hibernate framework
5. Create POJO Class
- Create Hibernate managed Entity class (regular POJO class)
- Declare the class as an entity using either:
- XML tags (e.g.,
User.hbm.xml
- one file per entity)
- OR
- JPA annotations from
jakarta.persistence
package:
- Mandatory annotations:
@Entity
- class level annotation
@Id
- field level or getter (property) level annotation
6. Add Mapping Entry per Entity