Hibernate二级缓存示例 [英] Hibernate second level cache example

查看:95
本文介绍了Hibernate二级缓存示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
@Table(name =pizza)
public class Pizza实现Serializable {
@Id
@GeneratedValue
private Integer id;
私人字符串名称;
私人双重价格;

public long getId(){
return id;
}

public void setId(Integer id){
this.id = id;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public double getPrice(){
return price;
}

public void setPrice(double price){
this.price = price;
}

}

ehcache.xml

 < cache name =com.abp.osp.domain.Pizza
maxElementsInMemory =100
eternal =false
timeToIdleSeconds =5
timeToLiveSeconds =200/>
< / ehcache>

我在bean.xml中提到了ehcache

 < prop key =hibernate.cache.provider_configuration_file_resource_path> ehcache.xml< / prop> 
< prop key =hibernate.cache.use_second_level_cache> true< / prop>
< prop key =hibernate.cache.use_query_cache> true< / prop>
< prop key =hibernate.cache.region.factory_class> net.sf.ehcache.hibernate.EhCacheRegionFactory< / prop>
< prop key =hibernate.cache.provider_class> net.sf.ehcache.hibernate.EhCacheProvider< / prop>

以及我在dao类中的调用方法是

  Session session1 = sessionFactory.openSession(); 

比萨pizza2 =(比萨)session1.load(Pizza.class,2);
System.out.println(pizza2 - + pizza2.getName());
session1.close();

会话session2 = sessionFactory.openSession();

比萨pizza4 =(比萨)session2.load(Pizza.class,2);
System.out.println(pizza4 - + pizza4.getName());
session2.close();

输出结果为:

  Hibernate:select pizza0_.id as id0_0_,pizza0_.name as name0_0_,pizza0_.price as price0_0_ from pizza pizza0_ where pizza0_.id =? 
pizza2 - 薄壳
Hibernate:选择pizza0_.id作为id0_0_,pizza0_.name作为name0_0_,pizza0_.price作为price0_0_从pizza pizza0_其中pizza0_.id =?
pizza4 - Thin Crust

但它在数据库中击中了两次。我没有找到我的代码中有任何错误,请告诉我为什么它在数据库中击中了两次。

添加

  @Cache(usage = CacheConcurrencyStrategy.READ_ONLY,region =pizza)

在域名类中。

  @Entity 
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY,region =pizza)
@Table(name =pizza)
public class Pizza实现Serializable {


I am developing hibernate + ehcache program.

@Entity
@Table(name = "pizza")
public class Pizza implements Serializable{
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private double price;

    public long getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

}

ehcache.xml

  <cache name="com.abp.osp.domain.Pizza"   
maxElementsInMemory="100"   
eternal="false"   
timeToIdleSeconds="5"   
timeToLiveSeconds="200" /> 
</ehcache>

I have mentioned ehcache in bean.xml

   <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
        <prop key="hibernate.cache.use_second_level_cache">true</prop>
        <prop key="hibernate.cache.use_query_cache">true</prop>
        <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
         <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>

and my calling method within dao class is

 Session session1=sessionFactory.openSession();  

          Pizza pizza2=(Pizza)session1.load(Pizza.class, 2);
          System.out.println("pizza2--"+pizza2.getName());  
          session1.close(); 

 Session session2=sessionFactory.openSession();  

          Pizza pizza4=(Pizza)session2.load(Pizza.class, 2);
          System.out.println("pizza4--"+pizza4.getName());  
          session2.close(); 

Output is:

Hibernate: select pizza0_.id as id0_0_, pizza0_.name as name0_0_, pizza0_.price as price0_0_ from pizza pizza0_ where pizza0_.id=?
pizza2--Thin Crust
Hibernate: select pizza0_.id as id0_0_, pizza0_.name as name0_0_, pizza0_.price as price0_0_ from pizza pizza0_ where pizza0_.id=?
pizza4--Thin Crust

But it hit twice in database.I don't find anything wrong in my code.Please suggest me why it hit twice in database.

解决方案

I have solved my question.I need to add

@Cache(usage=CacheConcurrencyStrategy.READ_ONLY,region="pizza")

In domain class.

@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY,region="pizza")
@Table(name = "pizza")
public class Pizza implements Serializable{

这篇关于Hibernate二级缓存示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆