A couple of examples are given to shows Hibernate first level cache scenario.
Database Table:
1 2 3 4 5 6 7 8 9 10 11 |
DROP TABLE IF EXISTS `mytecbar`.`category`; CREATE TABLE `mytecbar`.`category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL, `description` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; INSERT INTO `category` VALUES (3,'car','2015 models'),(4,'magazine','2000 options'); |
Here are we are using XML Mappling without Annotations)
Domain class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package net.tecbar.domain; // import java.util.Set; public class Category { private int id; private String name; private String description; 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 getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "PIAccount [id=" + id + ", name=" + name + ", description=" + description + "]"; } } |
XML Mapping file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="net.tecbar.domain.Category" table="category"> <id name="id" type="integer"> <column name="id" /> <generator class="native"></generator> </id> <property name="name" type="string"> <column name="name" /> </property> <property name="description" type="string"> <column name="description" /> </property> </class> </hibernate-mapping> |
hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Settings for a remote MySQL database. --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/mytecbar?autoReconnect=true</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.autocommit">true</property> <property name="c3p0.min_size">3</property> <property name="c3p0.max_size">6</property> <property name="c3p0.timeout">1000</property> <property name="c3p0.idle_test_period">2000</property> <property name="c3p0.preferredTestQuery">select 1;</property> <!-- Use EHCache for cache--> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <property name="cache.provider_configuration_file_resource_path">/ehcache.xml</property> <property name="cache.use_query_cache">true</property> <property name="cache.use_minimal_puts">false</property> <property name="max_fetch_depth">3</property> <!-- Print SQL to stdout. --> <property name="show_sql">true</property> <property name="use_sql_comments">true</property> <!-- Mapping files --> <mapping resource="net/tecbar/domain/category.hbm.xml" /> </session-factory> </hibernate-configuration> |
Example Code 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package net.tecbar.hibernate; import net.tecbar.domain.Category; public class HibernateFirstLevelCacheExample { public static void main(String[] args) { // query database and put it into first level cache final Category category = (Category)HibernateUtil.getSession().get(Category.class, 3); System.out.println(category); // will check first level cache without querying database final Category category2 = (Category)HibernateUtil.getSession().get(Category.class, 3); System.out.println(category2); HibernateUtil.closeSession(); } } |
Output:
1 2 3 4 5 |
Hibernate: select category0_.id as id1_0_0_, category0_.name as name2_0_0_, category0_.description as descript3_0_0_ from category category0_ where category0_.id=? PIAccount [id=3, name=car, description=2015 models] PIAccount [id=3, name=car, description=2015 models] |
Example Code 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package net.tecbar.hibernate; import net.tecbar.domain.Category; public class HibernateFirstLevelCacheExample2 { public static void main(String[] args) { // query database and put it into first level cache final Category category = (Category)HibernateUtil.getSession().get(Category.class, 3); System.out.println(category); HibernateUtil.closeSession(); // The entity object was gone after the previous session close. // query database again, and put the result into first level cache associate with new session final Category category2 = (Category)HibernateUtil.getSession().get(Category.class, 3); System.out.println(category2); HibernateUtil.closeSession(); } } |
Output:
1 2 3 4 5 6 |
Hibernate: select category0_.id as id1_0_0_, category0_.name as name2_0_0_, category0_.description as descript3_0_0_ from category category0_ where category0_.id=? PIAccount [id=3, name=car, description=2015 models] Hibernate: select category0_.id as id1_0_0_, category0_.name as name2_0_0_, category0_.description as descript3_0_0_ from category category0_ where category0_.id=? PIAccount [id=3, name=car, description=2015 models] |
Pingback: Hibernate Caching
Pingback: Hibernate Second Level Cache Example