SlideShare a Scribd company logo
HIBERNATE
CONFIGURATION WITH
ANNOTAION
Hibernate Configuration with Annotations
• Hibernate is an Object-Relational Mapping
(ORM) framework for Java
• Allows you to map Java objects to database
tables and vice versa.
• When using Hibernate with annotations, you can
configure the mapping between your Java
classes and the database tables without the need
for XML configuration files.
Step-by-step Guide To Configuring
Hibernate Using Annotations:
1. Set up the Hibernate dependencies:
– Make sure you have the necessary Hibernate
dependencies in your project.
– This typically includes the Hibernate core library, the
appropriate database driver for your database, and the
Java Persistence API (JPA) libraries.
2. Configure the Hibernate properties:
– Create a properties file (e.g., hibernate.properties) or a
configuration class to set up the Hibernate properties
Example using properties file
# Database connection properties
hibernate.connection.driver_class = your.database.driver.Class
hibernate.connection.url =
jdbc:mysql://localhost:3306/your_database
hibernate.connection.username = your_username
hibernate.connection.password = your_password
# Hibernate properties
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto = update
3. Create the Hibernate configuration
class:
– You can create a configuration class that
will initialize the Hibernate configuration
using the annotations.
– Here's an example of a configuration class:
import org.hibernate.cfg.Configuration;
public class HibernateConfig {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
// Create a Configuration instance
Configuration configuration = new Configuration();
// Configure Hibernate using the properties file
configuration.configure("hibernate.properties");
// Add annotated classes
configuration.addAnnotatedClass(YourEntityClass1.class);
configuration.addAnnotatedClass(YourEntityClass2.class);
// Add other entity classes as needed
// Build the SessionFactory
sessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
return sessionFactory;
}
}
4. Define your entity classes:
– Create your entity classes, which
represent your database tables.
– Annotate them with Hibernate
annotations to define the mapping
between the classes and the database
tables
– Example follows:
Example
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class YourEntityClass {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// Other properties and getters/setters
}
5. Perform database operations:
– You can now use the SessionFactory to
create Session instances
– And perform database operations.
– Example Follows :
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateConfig.getSessionFactory();
Session session = sessionFactory.openSession();
try {
Transaction transaction = session.beginTransaction();
// Perform database operations
YourEntityClass entity = new YourEntityClass();
entity.setProperty("value");
session.save(entity);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
}
}

More Related Content

PDF
Hibernate Presentation
PDF
hibernate-presentation-1196607644547952-4.pdf
PPTX
Hibernate
PDF
Hibernate presentation
PPT
Hibernate
PPT
5-Hibernate.ppt
ODP
Hibernate complete Training
PPT
Basic Hibernate Final
Hibernate Presentation
hibernate-presentation-1196607644547952-4.pdf
Hibernate
Hibernate presentation
Hibernate
5-Hibernate.ppt
Hibernate complete Training
Basic Hibernate Final

Similar to Session 3 - hibernate - Configuration with Annotations.pptx (20)

PPT
hibernate with JPA
PDF
Hibernate Tutorial
DOCX
4.3 Hibernate example.docx
PPTX
Hibernate example1
PPT
PPTX
Spring & hibernate
PPT
Hibernate for Beginners
PPTX
Hibernate tutorial
PPTX
Hibernate
PDF
Hibernate 3
PPTX
Introduction to Hibernate Framework
PPTX
Introduction to Hibernate Framework
PPTX
Hibernate ppt
PPTX
Hibernate in XPages
PPT
Hibernate Tutorial
PPTX
Hibernate
PPT
Hibernate training-topics
ODP
Hibernate 18052012
PPT
Hibernate presentation
hibernate with JPA
Hibernate Tutorial
4.3 Hibernate example.docx
Hibernate example1
Spring & hibernate
Hibernate for Beginners
Hibernate tutorial
Hibernate
Hibernate 3
Introduction to Hibernate Framework
Introduction to Hibernate Framework
Hibernate ppt
Hibernate in XPages
Hibernate Tutorial
Hibernate
Hibernate training-topics
Hibernate 18052012
Hibernate presentation
Ad

More from ASRPANDEY (11)

PPTX
finalpresentationonchatbot-200620130813.pptx
PPTX
14_04_transformerso3459834759883457983475.pptx
PPTX
lec0734523532453425324523452345245432.pptx
PPTX
Agglomerative Clustering asdfAlgorithm.pptx
PPTX
Supervised Learning Algorithmswith better lifecycle.pptx
PPTX
AZURE CLOUD ARCHITECTUREfggggggggdfgdg.pptx
PPT
Topic5ClassDesignBestPPtFileForGCPANDALLCLOud.ppt
PDF
DOC-20240726-WA0003sdjfdfngsdfgbmsdfvdsf..pdf
PPT
abm-issuesBestPPtOnCouplingAndIt'sDegrees.ppt
PPTX
Copy of Azure Fundamentals AZ 900THeBest.pptx
PPTX
MODULE-2.pptx
finalpresentationonchatbot-200620130813.pptx
14_04_transformerso3459834759883457983475.pptx
lec0734523532453425324523452345245432.pptx
Agglomerative Clustering asdfAlgorithm.pptx
Supervised Learning Algorithmswith better lifecycle.pptx
AZURE CLOUD ARCHITECTUREfggggggggdfgdg.pptx
Topic5ClassDesignBestPPtFileForGCPANDALLCLOud.ppt
DOC-20240726-WA0003sdjfdfngsdfgbmsdfvdsf..pdf
abm-issuesBestPPtOnCouplingAndIt'sDegrees.ppt
Copy of Azure Fundamentals AZ 900THeBest.pptx
MODULE-2.pptx
Ad

Recently uploaded (20)

PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
introduction to datamining and warehousing
PDF
composite construction of structures.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Safety Seminar civil to be ensured for safe working.
DOCX
573137875-Attendance-Management-System-original
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Sustainable Sites - Green Building Construction
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Digital Logic Computer Design lecture notes
PDF
PPT on Performance Review to get promotions
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Artificial Intelligence
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
introduction to datamining and warehousing
composite construction of structures.pdf
Foundation to blockchain - A guide to Blockchain Tech
Safety Seminar civil to be ensured for safe working.
573137875-Attendance-Management-System-original
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CYBER-CRIMES AND SECURITY A guide to understanding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Sustainable Sites - Green Building Construction
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Digital Logic Computer Design lecture notes
PPT on Performance Review to get promotions
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Artificial Intelligence

Session 3 - hibernate - Configuration with Annotations.pptx

  • 2. Hibernate Configuration with Annotations • Hibernate is an Object-Relational Mapping (ORM) framework for Java • Allows you to map Java objects to database tables and vice versa. • When using Hibernate with annotations, you can configure the mapping between your Java classes and the database tables without the need for XML configuration files.
  • 3. Step-by-step Guide To Configuring Hibernate Using Annotations: 1. Set up the Hibernate dependencies: – Make sure you have the necessary Hibernate dependencies in your project. – This typically includes the Hibernate core library, the appropriate database driver for your database, and the Java Persistence API (JPA) libraries. 2. Configure the Hibernate properties: – Create a properties file (e.g., hibernate.properties) or a configuration class to set up the Hibernate properties
  • 4. Example using properties file # Database connection properties hibernate.connection.driver_class = your.database.driver.Class hibernate.connection.url = jdbc:mysql://localhost:3306/your_database hibernate.connection.username = your_username hibernate.connection.password = your_password # Hibernate properties hibernate.dialect = org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto = update
  • 5. 3. Create the Hibernate configuration class: – You can create a configuration class that will initialize the Hibernate configuration using the annotations. – Here's an example of a configuration class:
  • 6. import org.hibernate.cfg.Configuration; public class HibernateConfig { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { if (sessionFactory == null) { try { // Create a Configuration instance Configuration configuration = new Configuration(); // Configure Hibernate using the properties file configuration.configure("hibernate.properties"); // Add annotated classes configuration.addAnnotatedClass(YourEntityClass1.class); configuration.addAnnotatedClass(YourEntityClass2.class); // Add other entity classes as needed // Build the SessionFactory sessionFactory = configuration.buildSessionFactory(); } catch (Throwable ex) { System.err.println("SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } return sessionFactory; } }
  • 7. 4. Define your entity classes: – Create your entity classes, which represent your database tables. – Annotate them with Hibernate annotations to define the mapping between the classes and the database tables – Example follows:
  • 8. Example import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class YourEntityClass { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // Other properties and getters/setters }
  • 9. 5. Perform database operations: – You can now use the SessionFactory to create Session instances – And perform database operations. – Example Follows :
  • 10. import org.hibernate.Session; import org.hibernate.Transaction; public class Main { public static void main(String[] args) { SessionFactory sessionFactory = HibernateConfig.getSessionFactory(); Session session = sessionFactory.openSession(); try { Transaction transaction = session.beginTransaction(); // Perform database operations YourEntityClass entity = new YourEntityClass(); entity.setProperty("value"); session.save(entity); transaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); sessionFactory.close(); } } }