Hibernate二级缓存示例

Hibernate二级缓存示例,hibernate,ehcache,Hibernate,Ehcache,我正在开发hibernate+ehcache程序 @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;

我正在开发hibernate+ehcache程序

@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>
输出为:

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

但它在数据库中命中两次。我在代码中没有发现任何错误。请告诉我为什么它在数据库中命中两次。

我已经解决了我的问题。我需要添加

@Cache(usage=CacheConcurrencyStrategy.READ_ONLY,region="pizza")
在域类中

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