MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

Hibernate

Program Of EHCache
Previous Home Next

Example:

This example shows the implementation of EHCache

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">

<!-- Generated by MyEclipse Hibernate Tools.   -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">system</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="hibernate.cache.provider_class"
>net.sf.ehcache.hibernate.EhCacheProvider
</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernete.ca"></property>
<mapping resource="m-to-m.hbm.xml"/>
</session-factory>
</hibernate-configuration>

m-to-m.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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-mapping>
<class name="mypack.Previlige">
<cache usage="read-write"/>
<id name="id" type="int">
<generator class="increment" />
</id>
<property name="name"/>
<property name="cost" type="int"/>
<set name="users" table="empPrevilige" inverse="true">
<key column="previligeId"/>
<many-to-many class="mypack.Emp" column="empId" />
</set>
</class>
<class name="mypack.Emp">
<cache usage="read-write"/>
<id name="id" type="int">
<generator class="sequence" >
<param name="sequence">keygenerator</param>
</generator>
</id>
<property name="name" />
<property name="job" />
<property name="salary" type="int"/>
<set name="previliges" table="empPrevilige" cascade="all">
<cache usage="read-write"/>
<key column="empId"/>
<many-to-many class="mypack.Previlige" column="previligeId" />
</set>
</class>
</hibernate-mapping>

ehcache.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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-mapping>
<class name="mypack.Previlige">
<cache usage="read-write"/>
<id name="id" type="int">
<generator class="increment" />
</id>
<property name="name"/>
<property name="cost" type="int"/>
<set name="users" table="empPrevilige" inverse="true">
<key column="previligeId"/>
<many-to-many class="mypack.Emp" column="empId" />
</set>
</class>
<class name="mypack.Emp">
<cache usage="read-write"/>
<id name="id" type="int">
<generator class="sequence" >
<param name="sequence">keygenerator</param>
</generator>
</id>
<property name="name" />
<property name="job" />
<property name="salary" type="int"/>
<set name="previliges" table="empPrevilige" cascade="all">
<cache usage="read-write"/>
<key column="empId"/>
<many-to-many class="mypack.Previlige" column="previligeId" />
</set>
</class>
</hibernate-mapping>

Emp.java

package r4r;
import java.util.*;
public class Emp {

int id;
String name,job;
int salary;
Set<Previlige> priviliges;
public Emp() {
super();
}
public Emp(String name, String job, int salary, Set<Previlige> priviliges) {
super();
this.name = name;
this.job = job;
this.salary = salary;
this.priviliges = priviliges;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Set<Previlige> getPriviliges() {
return priviliges;
}
public void setPriviliges(Set<Previlige> priviliges) {
this.priviliges = priviliges;
}
}

Previlage.java

package r4r;

import java.util.Set;
public class Previlige {
int id;
String name;
int cost;
Set<Emp> users;
public Previlige() {
super();
}
public Previlige(String name, int cost) {
super();
this.name = name;
this.cost = cost;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public Set<Emp> getUsers() {
return users;
}
public void setUsers(Set<Emp> users) {
this.users = users;
}
}

SessionProvider.java

package r4r;

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

public class SessionProvider {
static SessionFactory factory;
static
{
Configuration cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}
public static Session getSession() {
return factory.openSession();
}

}

S2Test.java

package r4r;

import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.stat.SecondLevelCacheStatistics;

public class S2Test {

public static void main(String[] args) {
try
{
Configuration cfg=new Configuration().configure();
SessionFactory f=cfg.buildSessionFactory();
for(int i=1;i<=2;i++)
{
Session s=f.openSession();
Date d1=new Date();
Emp e=(Emp)s.load(Emp.class, 52501);
Set<Previlige> set=e.getPriviliges();
Date d2=new Date();
System.out.println("query took "+(d2.getTime()-d1.getTime())+
" milliseconds "+i+" time.");
System.out.println("Emp Details:");
System.out.println(e.getName()+"\t"+e.getJob()+"\t"+
e.getSalary());
System.out.println("previliges  details...");
Iterator<Previlige> itr=set.iterator();
while(itr.hasNext())
{
Previlige pr=itr.next();
System.out.println(pr.getId()+"\t"+pr.getName()+"\t"
+pr.getCost());
}
s.close();
SecondLevelCacheStatistics sts1 = f.getStatistics().
getSecondLevelCacheStatistics("mypack.Emp");
System.out.println("details of Emp cache region:");
System.out.println(sts1);
SecondLevelCacheStatistics sts2 = f.getStatistics().
getSecondLevelCacheStatistics("mypack.Previlige");
System.out.println("details of previlige cache region:");
System.out.println(sts2);
}
}catch(Exception e)
{
System.out.println(e);
}
}

}
Previous Home Next