Hibernate_Spring_Integration
Hibernate_Spring_Integration
To start using Hibernate, we need to install the necessary libraries and configure Hibernate to work
There are two main ways to install Hibernate in your Java project:
2. Manual Installation (Without Maven): You manually download and add JAR files to your project.
1. Ensure Maven is installed. If not, download and install Maven from Maven's website
(https://maven.apache.org/).
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.9.Final</version>
</dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
</dependencies>
Hibernate Configuration
Once Hibernate is installed, configure it to connect to your database. You can do this via an XML file
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
@Configuration
@EnableTransactionManagement
@Bean
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("com.example.model");
return sessionFactory;
}
@Bean
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
dataSource.setUsername("root");
dataSource.setPassword("password");
return dataSource;
@Entity
@Table(name = "Employee")
@Id
private int id;
session.beginTransaction();
session.save(tempEmployee);
session.getTransaction().commit();
Hibernate can be integrated with Spring to simplify configuration and database operations.
- Simplified Code: Using HibernateTemplate from Spring removes the need for writing boilerplate
code.