Weekend -Special offer upto 50% off | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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
  • Transaction

JAVA Tutorial Video

[embed]https://www.youtube.com/watch?v=Mb8nG-NH6gg&t=2835s[/embed] Example/> 
Implementationread-onlynonstrict-read-writeread-writetransactional
EH CacheYesYesYesNo
OS CacheYesYesYesNo
Swarm CacheYesYesNoNo
JBoss CacheNoNoNoYes
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.EhCacheProviderproperty><property name="hibernate.cache.use_second_level_cache">trueproperty>2) Add cache usage setting in hbm file        <cache usage="read-only" />3) Create ehcache.xml file        ?>        <ehcache>  <defaultCachemaxElementsInMemory="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 configurationConfiguration cfg=new Configuration();cfg.configure("hibernate.cfg.xml");//creating seession factory objectSessionFactory factory=cfg.buildSessionFactory();//creating session objectSession session=factory.openSession();//creating transaction objectTransaction t=session.beginTransaction();Employee e1=new Employee(111,"arun",40000);session.persist(e1);//persisting the objectt.commit();//transaction is commitedsession.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 databaseCREATE TABLE  "EMP558"(    "ID" NUMBER(10,0) NOT NULL ENABLE,"NAME" VARCHAR2(255 CHAR),"SALARY" FLOAT(126),PRIMARY KEY ("ID") ENABLE)/ 2) Employee.javapackage com.javaspot;public class Employee {private int id;private String name;private float salary;//getters and setters}3) employee.hbm.xml<class name="com.javatpoint.Employee" table="emp558">class
="assigned">class
>4) EmployeeDao.javapackage 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 employeepublic void saveEmployee(Employee e){template.save(e);}//method to update employeepublic void updateEmployee(Employee e){template.update(e);}//method to delete employeepublic void deleteEmployee(Employee e){template.delete(e);}//method to return one employee of given idpublic Employee getById(int id){Employee e=(Employee)template.get(Employee.class,id);return e;}//method to return all employeespublic List getEmployees(){List list=new ArrayList();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 hibernate Properties. The protest of LocalSessionFactoryBean class is utilized as a part of the Hibernate Template class.How about we see the code of applicationContext.xml document.

Hibernate Template Class

File: applicationContext.xmlclass="org.apache.commons.dbcp.BasicDataSource">class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">                   employee.hbm.xmlorg.hibernate.dialect.Oracle9Dialectupdatetrueclass="org.springframework.orm.hibernate3.HibernateTemplate">      class="com.javaspot.EmployeeDao">6) InsertTest.javapackage 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 org.hibernate.dialect.Oracle9Dialectupdatetrue