Ad Code

✨🎆 Diwali Dhamaka Offer! 🎆✨

Get 20% OFF on All Courses at Shiva Concept Solution click

Hibernate cache Code


Hibernate uses the cache concept to reduce the roundtrip from the database server if we want to increase the performance of the application.

Type of cache

1)  Primary Cache:-  It will be created implicitly.

2)  Secondary Cache:-  It will be created explicitly.

create ehcache.xml

<?xml version="1.0"?>

<ehcache>

<defaultCache maxElementsInMemory="100" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="200" />

<cache name="scs.Job" maxElementsInMemory="100" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="200" />

</ehcache>

Code under hibernate.cfg.xml:-

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

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

<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql://localhost:3306/test</property>

<property name="connection.username">root</property>

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

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

<mapping resource="job.hbm.xml"></mapping>

</session-factory>

</hibernate-configuration>

hibernate.hbm.xml

.<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

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

<hibernate-mapping>

<class name="scs.Job" table="tbl_job">

<cache usage="read-only" />

<id name="jobid" column="jobid">

<generator class="assigned"></generator>

</id>

<property name="jobtitle" />

</class>

<query name="Myquery">

<![CDATA[from Job p where p.jobid>:a ]]>

</query>

</hibernate-mapping>

Viewjob.java:-

package scs;

import java.util.Iterator;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class ViewJob {

public static void main(String[] args) {

Configuration cfg = new Configuration();

cfg.configure("hibernate.cfg.xml");

    SessionFactory sf = cfg.buildSessionFactory();

    Session s = sf.openSession();

  //  Query q = s.createQuery("from Job j"); //select * from job

  //  Query q = s.createQuery("select j.jobid,j.jobtitle from Job j"); //select * from job

   // Query q = s.createQuery("select j.jobid,j.jobtitle from Job j where j.jobid=?"); //select * from job

   // q.setInteger(0,5);

//    Query q = s.createQuery("select j.jobid,j.jobtitle from Job j where j.jobid=:a and j.jobtitle=:b"); //select * from job

//    Query q = s.createSQLQuery("select * from tbl_job");

    Query q = s.getNamedQuery("Myquery");

    q.setParameter("a", new Integer(5));

// q.setInteger("a",5);

     // q.setString("b","Professor");

    List lst = q.list();

    Iterator it = lst.iterator();

    while(it.hasNext())

    {

    Job obj =(Job) it.next();

    System.out.println(obj.getJobid() + " "+obj.getJobtitle());

    //  Object arr[] =(Object[])it.next();

    //.out.println(arr[0] + "," + arr[1]);

    //System.out.println(it.next());

    }

    s.close();

}

}


Post a Comment

0 Comments