Second Level Cache

Hibernate Second Level Cache:

Second Level Cache utilizes a typical reserve for all the session question of a session industrial facility. It is valuable on the off chance that you have various session objects from a session factory. Session Factory holds the second level store information. It is worldwide for all the session questions and not empowered of course.

Different sellers have given the execution of Second Level Cache.
  1. EH Cache
  2. OS Cachen
  3. Swarm Cache
  4. JBoss Cache

Different cache usage functionality

  • Read-only
  • Nonstrict-read-write
  • Read-write
  • Transactional

Example

<cache usage=”read-only” />

 

Implementation read-only nonstrict-read-write read-write transactional
EH Cache Yes Yes Yes No
OS Cache Yes Yes Yes No
Swarm Cache Yes Yes No No
JBoss Cache No No No Yes

3 extra steps for second level cache example using EH cache:

1) Add 2 configuration setting in hibernate.cfg.xml file

<property name=”cache.provider_class”>org.hibernate.cache.EhCacheProvider</property>

<property name=”hibernate.cache.use_second_level_cache”>true</property>

2) Add cache usage setting in hbm file

        <cache usage=”read-only” />

3) Create ehcache.xml file

        <?xml version=”1.0″?>

        <ehcache>

  <defaultCache

maxElementsInMemory=”100″

eternal=”true”/>

 </ehcache>

 Hibernate and Struts 2 Integration:

We can coordinate any struts application with hibernate. There is no prerequisite of additional endeavors. In this illustration, we going to utilize struts 2 structure with hibernate. You need jostle documents for struts 2 and rest.

Example of Hibernate and struts2 integration

  • index.jsp
  •  User.java
  • RegisterDao.java
  •  user.hbm.xml
  •  hibernate.cfg.xml
  • struts.xml
  • welcome.jsp
  • web.xml

Hibernate and Spring Integration:

We can basically incorporate hibernate application with spring application. In hibernate structure , we give all the database data hibernate.cfg.xml document. Be that as it may, in the event that we will coordinate the hibernate application with spring; we don’t have to make the hibernate.cfg.xml record. We can give all the data in the application Context.xml record.

 Advantage of Spring framework with hibernate:

The Spring structure gives Hibernate Template class, so you don’t have to take after such a variety of steps like make Configuration, BuildSessionFactory, Session, starting and conferring exchange.

Using the Problem without spring :

//creating configuration

Configuration cfg=new Configuration();

cfg.configure(“hibernate.cfg.xml”);

//creating seession factory object

SessionFactory factory=cfg.buildSessionFactory();

//creating session object

Session session=factory.openSession();

//creating transaction object

Transaction t=session.beginTransaction();

Employee e1=new Employee(111,”arun”,40000);

session.persist(e1);//persisting the object

t.commit();//transaction is commited

session.close();

Answer by using Template class of Spring :

Employee e1=new Employee(111,”arun”,40000);

hibernateTemplate.save(e1);

Methods of HibernateTemplate class:

No. Method
1) void persist(Object entity)
2) Serializable save(Object entity)
3) void saveOrUpdate(Object entity)
4) void update(Object entity)
5) void delete(Object entity)
6) Object get(Class entityClass, Serializable id)
7) Object load(Class entityClass, Serializable id)
8) List loadAll(Class entityClass)

Steps:

  1. create table in the database
  2.  create applicationContext.xml file
  3.  create Employee.java file
  4. create employee.hbm.xml file
  5.  create EmployeeDao.java file
  6. create InsertTest.java file

1) create the table in the database

CREATE TABLE  “EMP558”

(    “ID” NUMBER(10,0) NOT NULL ENABLE,

“NAME” VARCHAR2(255 CHAR),

“SALARY” FLOAT(126),

PRIMARY KEY (“ID”) ENABLE

)

/

 2) Employee.java

package com.javaspot;

public class Employee {

private int id;

private String name;

private float salary;

//getters and setters

}

3) employee.hbm.xml

<?xml version=’1.0′ encoding=’UTF-8′?>

<!DOCTYPE hibernate-mapping PUBLIC

“-//Hibernate/Hibernate Mapping DTD 3.0//EN”

“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>

<hibernate-mapping>

<class name=”com.javatpoint.Employee” table=”emp558″>

<id name=”id”>

<generator class=”assigned”></generator>

</id>

<property name=”name”></property>

<property name=”salary”></property>

</class>

</hibernate-mapping>

4) EmployeeDao.java

package com.javaspot;

import org.springframework.orm.hibernate3.HibernateTemplate;

import java.util.*;

public class EmployeeDao {

HibernateTemplate template;

public void setTemplate(HibernateTemplate template) {

this.template = template;

}

//method to save employee

public void saveEmployee(Employee e){

template.save(e);

}

//method to update employee

public void updateEmployee(Employee e){

template.update(e);

}

//method to delete employee

public void deleteEmployee(Employee e){

template.delete(e);

}

//method to return one employee of given id

public Employee getById(int id){

Employee e=(Employee)template.get(Employee.class,id);

return e;

}

//method to return all employees

public List<Employee> getEmployees(){

List<Employee> list=new ArrayList<Employee>();

list=template.loadAll(Employee.class);

return list;

}

}

5) applicationContext.xml:

In this record, we are giving every one of the informations of the database in the BasicDataSource protest. This question is utilized as a part of the LocalSessionFactoryBean class protest, containing some different informations, for example, mappingResources and hibernateProperties. The protest of LocalSessionFactoryBean class is utilized as a part of the HibernateTemplate class.How about we see the code of applicationContext.xml document.

Hibernate Template Class

File: applicationContext.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans

xmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xmlns:p=”http://www.springframework.org/schema/p”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>

<property name=”driverClassName”  value=”oracle.jdbc.driver.OracleDriver”></property>

<property name=”url” value=”jdbc:oracle:thin:@localhost:1521:xe”></property>

<property name=”username” value=”system”></property>

<property name=”password” value=”oracle”></property>

</bean>

<bean id=”mysessionFactory”  class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>

<property name=”dataSource” ref=”dataSource”></property>                   <property name=”mappingResources”>

<list>

<value>employee.hbm.xml</value>

</list>

</property>

<property name=”hibernateProperties”>

<props>

<prop key=”hibernate.dialect”>org.hibernate.dialect.Oracle9Dialect</prop>

<prop key=”hibernate.hbm2ddl.auto”>update</prop>

<prop key=”hibernate.show_sql”>true</prop>

</props>

</property>

</bean>

<bean id=”template” class=”org.springframework.orm.hibernate3.HibernateTemplate”>      <property name=”sessionFactory” ref=”mysessionFactory”></property>

</bean>

<bean id=”d” class=”com.javaspot.EmployeeDao”>

<property name=”template” ref=”template”></property>

</bean>

</beans>

6) InsertTest.java

package com.javaspot;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

public class InsertTest {

public static void main(String[] args) {

Resource r=new ClassPathResource(“applicationContext.xml”);

BeanFactory factory=new XmlBeanFactory(r);

EmployeeDao dao=(EmployeeDao)factory.getBean(“d”);

Employee e=new Employee();

e.setId(114);

e.setName(“varun”);

e.setSalary(50000);

dao.saveEmployee(e);

}

}

Enabling automatic table creation, showing sql queries

 <property name=”hibernateProperties”>

<props>

<prop key=”hibernate.dialect”>org.hibernate.dialect.Oracle9Dialect</prop>

<prop key=”hibernate.hbm2ddl.auto”>update</prop>

<prop key=”hibernate.show_sql”>true</prop>

</props>

 
Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.