You are on page 1of 5

MAIN.

JAVA:-

package com.tut.HibernateEhcache;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Main {


public static void main(String[] args) {
// Configure Hibernate
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(User.class);
SessionFactory sessionFactory = configuration.buildSessionFactory();

// Insert six records into the database


insertRecords(sessionFactory);

// Activate second-level cache


configuration.setProperty("hibernate.cache.use_second_level_cache", "true");
configuration.setProperty("hibernate.cache.region.factory_class",
"org.hibernate.cache.ehcache.EhCacheRegionFactory");

// Fetch objects using distinct session instances


Session session1 = sessionFactory.openSession();
Session session2 = sessionFactory.openSession();

// Fetch objects from the database (First Session)


System.out.println("Fetching objects from the database (First Session):");
for (int i = 1; i <= 6; i++) {
User user = session1.get(User.class, i);
System.out.println(user);
}

// Fetch objects from the cache (Second Session)


System.out.println("\nFetching objects from the cache (Second Session):");
for (int i = 1; i <= 6; i++) {
User user = session2.get(User.class, i);
System.out.println(user);
}

// Close sessions and session factory


session1.close();
session2.close();
sessionFactory.close();
}

private static void insertRecords(SessionFactory sessionFactory) {


Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
for (int i = 1; i <= 6; i++) {
User user = new User();
user.setUserId(i);
user.setUsername("User" + i);
user.setAddress("Address" + i);
user.setSalary(50000 * i);
session.save(user);
}

transaction.commit();
session.close();
}
}

USER.JAVA

package com.tut.HibernateEhcache; // Adjust the package name as needed

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {
@Id
private int userId;
private String username;
private String address;
private double salary;

// Constructors
public User() {}

public User(int userId, String username, String address, double salary) {


this.userId = userId;
this.username = username;
this.address = address;
this.salary = salary;
}

// Getter and setter methods


public int getUserId() {
return userId;
}

public void setUserId(int userId) {


this.userId = userId;
}

public String getUsername() {


return username;
}

public void setUsername(String username) {


this.username = username;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public double getSalary() {


return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}
}

Hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD


3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection properties -->
<property
name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/myhiber</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">2002utk</property>

<!-- Hibernate dialect for MySQL -->


<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->


<property name="hibernate.current_session_context_class">thread</property>

<!-- Enable Hibernate's automatic session context management -->


<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Mapping file -->


<mapping class="com.tut.HibernateEhcache.User"/> <!-- Adjust the package name
and class name as needed -->
</session-factory>
</hibernate-configuration>

POM.XML:-
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.tut</groupId>
<artifactId>HibernateEhcache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>HibernateEhcache</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- Hibernate dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.5.7.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version> <!-- Adjust version as needed -->
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.5.7.Final</version>
</dependency>

<!-- Ehcache dependency -->


<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Output:-

You might also like