Second
level Caching- Across multiple sessions. Even if session is closed,
the object would be saved in second level cache and if called again, will be
directly accessed from there only.
Types of second level cache provider in hibernate:
1)EhCacheProvider
2) OSCacheProvider
Configuration changes in hibernate.cfg.xml
1. <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
2. <property name="hibernate.cache.use_second_level_cache">true</property>
Person.java
@Entity(name="PERSON")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Person implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
hibernate.cfg.cml
<hibernate-configuration>
<session-factory>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@172.16.7.236:20300:prtp</property>
<property name="connection.username">MOBI_CLEANUP_TEST1</property>
<property name="connection.password">MOBI_CLEANUP_TEST1</property>
<!-- JDBC connection pool (use
the built-in) -->
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- Enable Hibernate's automatic
session context management, by default
its update -->
<property name="hbm2ddl.auto">update</property>
<!-- Echo all executed SQL to
stdout -->
<property name="show_sql">true</property>
<!-- <mapping
class="org.javadsalgo.dto.UserDetailsDTO"/> <mapping
class="org.javadsalgo.dto.Vehicle"/>
<mapping
class="org.javadsalgo.dto.TwoWheeler"/> <mapping
class="org.javadsalgo.dto.FourWheeler"/> -->
<mapping class="org.javadsalgo.dto.Person" />
<!-- <mapping
resource="org/javadsalgo/model/UserDetailsDTO.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>
Output:
Hibernate: select
person0_.id as id0_0_, person0_.name as name0_0_ from PERSON person0_ where
person0_.id=?
No comments:
Post a Comment