Saturday, April 21, 2012

Hibernate Ecache Example


/**
 *
 */
package com.venkat.hibernate3;

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

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * @author Venkat
 *
 */
@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Cricket {

private String name;
private int runs;
@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRuns() {
return runs;
}
public void setRuns(int runs) {
this.runs = runs;
}


}
---------------------------------------------------------------------------------------------
/**
 * 
 */
package com.venkat.hibernate3;

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

/**
 * @author Venkat
 *
 */
public class HiberateTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session s=sf.openSession();
s.beginTransaction();
Cricket c=new Cricket();
c.setName("Venkat");
c.setRuns(9877);
s.save(c);
Cricket c1=new Cricket();
c1.setName("Dhoni");
c1.setRuns(8877);
s.save(c1);
Cricket c2=new Cricket();
c2.setName("Sunil");
c2.setRuns(9997);
s.save(c2);
s.getTransaction().commit();
s.close();
c=null;
s=sf.openSession();
c=(Cricket) s.get(Cricket.class, "Dhoni");
System.out.println("The runs of the Dhoni "+c.getRuns());
}

}
---------------------------------------------------------------------------------------------
<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.use_second_level_cache">true</property>
        <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
         <property name="hbm2ddl.auto">create</property> 
         <mapping class="com.venkat.hibernate3.Cricket"/>

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>

No comments:

Post a Comment